调用 java Web 服务 "REST" 需要使用来自 .Net C# 的用户名和密码以及证书进行身份验证

calling java web service "REST" that require auth with username & password and certificate from .Net C#

好的,这是我的故事,我有来自第三方的 java Web 服务,我必须使用 C# 连接到该服务 此服务需要以下内容

我的问题是此服务的供应商没有为该服务提供 WSDL link 并且证书文件是 java 密钥库我已经挂了 2 天并搜索这个在网络上没有运气

因此:我使用 SOAPUI 软件来调用此服务并且它运行良好

我想要的是, 是否可以在不向供应商请求 WSDL 的情况下使用此服务?如果是的话怎么办?

我们将不胜感激任何帮助。 提前致谢。

编辑: 服务 url 喜欢 https://10.111.10.12/ropnrs/Person?crn=1234567 而且它无法访问 throw web browser return 403 forbidden

您可以在没有 WSDL 的情况下使用 Web 服务,方法是使用 WebClient 发送 POST 请求,并使用 SOAP XML 消息填充请求的 body。

您可能需要添加额外的 HTTP headers。先用 SOAP UI 做一个请求,然后用 fiddler 查看请求。然后用 WebClient 发送请求。代码应该类似于:

using (var client = new WebClient())
{
    // this is the string containing the soap message
    var data = File.ReadAllText("request.xml");
    // HTTP header. set it to the identical working example you monitor with fiddler from SOAP UI
    client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
    // HTTP header. set it to the identical working example you monitor with fiddler from SOAP UI 
    client.Headers.Add("SOAPAction", "\"http://tempuri.org/ITestWcfService/TestMethod\"");
    // add/modify additional HTTP headers to make the request identical with what you get when calling from SOAP UI on a successful call 
    try
    {
        var response = client.UploadString("http://localhost:8080/TestWcfService", data);
    }
    catch (Exception e)
    {
        // handle exception                        
    }
}

我不确定证书,但也许您也可以通过某种方式将它设置到 Web 客户端。不幸的是,我对客户端证书知之甚少。但如果你能在评论中解释我也许能帮助你。如果您设法使用此解决方案使其工作,请在此 post 您的额外工作来处理证书问题。

WebClient 是 System.dll 程序集中 System.Net 的 class。