如何在 Asp.Net 中调用基于 HTTP 的 Java Web Service(Servlet)。

发布于 2024-12-11 20:25:55 字数 723 浏览 0 评论 0原文

您是否有任何想法在 asp.net.it 中调用 http(不是 https)Web 服务(使用用户名和密码进行身份验证)* 而不是 SOAP Web 服务。 Web服务是用java开发的。因此 wsdl.exe 无法通过引用进行调用。

这是 SOAP UI 提取的 SOAP 信封

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ak="http://linkaddress">
   <soapenv:Header>
         <ak:password>?</ak:password>
         <ak:username>?</ak:username>
   </soapenv:Header>
   <soapenv:Body>
         <ak:Vehicle>
            <chassisNo>?</chassisNo>
            <plateNo>?</plateNo>
            <plateCode>?</plateCode>
         </ak:Vehicle>
   </soapenv:Body>
</soapenv:Envelope>

Do you hav any idea abt calling http(not https) web service(with username and password for authentication )* in asp.net.its not SOAP web service. Web service is devloped in java. So wsdl.exe won't work nither calling though reference.

this is the SOAP UI extracted SOAP envelop

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ak="http://linkaddress">
   <soapenv:Header>
         <ak:password>?</ak:password>
         <ak:username>?</ak:username>
   </soapenv:Header>
   <soapenv:Body>
         <ak:Vehicle>
            <chassisNo>?</chassisNo>
            <plateNo>?</plateNo>
            <plateCode>?</plateCode>
         </ak:Vehicle>
   </soapenv:Body>
</soapenv:Envelope>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苦妄 2024-12-18 20:25:55

使用 Visual Studio 中的“添加 Web 服务引用”而不是“添加 Web 引用”。

Use Add Web Service Reference instead of Add Web Reference from Visual Studio.

自由范儿 2024-12-18 20:25:55

最后,我解决了我的问题:

string sResponse = string.Empty;
try {
    Uri uri = new Uri(sFetchURL);
    if (uri.Scheme == Uri.UriSchemeHttp) {

        HttpWebRequest request = null;
        request = (HttpWebRequest) HttpWebRequest.Create(uri);

        request.Method = WebRequestMethods.Http.Get;
        request.ContentType = "text/xml;charset=\"utf-8\"";

        string strSOAPRequestBody = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ak=\"http://Link.JavaService\">" +
         "<SOAP-ENV:Header>" +
          "<ak:password>" + myPassword + "</ak:password>" +
          "<ak:username>" + myUserName + "</ak:username>" +
         "</SOAP-ENV:Header>" +
         "<SOAP-ENV:Body>" +
          "<ak:Vehicle>" +
             "<chassisNo>" + sChessisNo + "</chassisNo>" +
             "<plateNo>" + sPlateNo + "</plateNo>" +
             "<plateCode>" + sPlateCode + "</plateCode>" +
          "</ak:passingVehicleTest>" +
         "</SOAP-ENV:Body>" +
        "</SOAP-ENV:Envelope>";

        request.Method = "POST";
        request.ContentType = "application/soap-xml; charset=UTF-8";
        request.Headers.Add("SOAPAction:\"\"");//please check RAW data In SOAP UI 

        request.ContentLength = strSOAPRequestBody.Length;
        System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(
                request.GetRequestStream());
        streamWriter.Write(strSOAPRequestBody);
        streamWriter.Close();
        System.IO.StreamReader streamReader = new System.IO.StreamReader(
                request.GetResponse().GetResponseStream());

        while (!streamReader.EndOfStream)
            sResponse += streamReader.ReadLine();
    }

} catch (WebException err) {
    HttpWebResponse httpResponse = null;
    httpResponse = (HttpWebResponse) err.Response;
    Stream baseStream = httpResponse.GetResponseStream();

    System.IO.StreamReader streamReader2 = new System.IO.StreamReader(
            baseStream);
    while (!streamReader2.EndOfStream)
        sResponse += streamReader2.ReadLine();
}

return sResponse;

Finally, I solve my problem:

string sResponse = string.Empty;
try {
    Uri uri = new Uri(sFetchURL);
    if (uri.Scheme == Uri.UriSchemeHttp) {

        HttpWebRequest request = null;
        request = (HttpWebRequest) HttpWebRequest.Create(uri);

        request.Method = WebRequestMethods.Http.Get;
        request.ContentType = "text/xml;charset=\"utf-8\"";

        string strSOAPRequestBody = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ak=\"http://Link.JavaService\">" +
         "<SOAP-ENV:Header>" +
          "<ak:password>" + myPassword + "</ak:password>" +
          "<ak:username>" + myUserName + "</ak:username>" +
         "</SOAP-ENV:Header>" +
         "<SOAP-ENV:Body>" +
          "<ak:Vehicle>" +
             "<chassisNo>" + sChessisNo + "</chassisNo>" +
             "<plateNo>" + sPlateNo + "</plateNo>" +
             "<plateCode>" + sPlateCode + "</plateCode>" +
          "</ak:passingVehicleTest>" +
         "</SOAP-ENV:Body>" +
        "</SOAP-ENV:Envelope>";

        request.Method = "POST";
        request.ContentType = "application/soap-xml; charset=UTF-8";
        request.Headers.Add("SOAPAction:\"\"");//please check RAW data In SOAP UI 

        request.ContentLength = strSOAPRequestBody.Length;
        System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(
                request.GetRequestStream());
        streamWriter.Write(strSOAPRequestBody);
        streamWriter.Close();
        System.IO.StreamReader streamReader = new System.IO.StreamReader(
                request.GetResponse().GetResponseStream());

        while (!streamReader.EndOfStream)
            sResponse += streamReader.ReadLine();
    }

} catch (WebException err) {
    HttpWebResponse httpResponse = null;
    httpResponse = (HttpWebResponse) err.Response;
    Stream baseStream = httpResponse.GetResponseStream();

    System.IO.StreamReader streamReader2 = new System.IO.StreamReader(
            baseStream);
    while (!streamReader2.EndOfStream)
        sResponse += streamReader2.ReadLine();
}

return sResponse;
甜柠檬 2024-12-18 20:25:55

尝试使用 HttpWebRequest

string url = "serviceurl";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest) System.Net.WebRequest
        .Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse) request
        .GetResponse();

if (response.StatusCode == System.Net.HttpStatusCode.OK) {
    System.IO.Stream receiveStream = response.GetResponseStream();
    System.IO.StreamReader readStream = null;

    if (response.CharacterSet == null)
        readStream = new System.IO.StreamReader(receiveStream);
    else
        readStream = new System.IO.StreamReader(receiveStream,

        System.Text.Encoding.GetEncoding(response.CharacterSet));

    string result = readStream.ReadToEnd();
    response.Close();
    readStream.Close();
}

Try using HttpWebRequest

string url = "serviceurl";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest) System.Net.WebRequest
        .Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse) request
        .GetResponse();

if (response.StatusCode == System.Net.HttpStatusCode.OK) {
    System.IO.Stream receiveStream = response.GetResponseStream();
    System.IO.StreamReader readStream = null;

    if (response.CharacterSet == null)
        readStream = new System.IO.StreamReader(receiveStream);
    else
        readStream = new System.IO.StreamReader(receiveStream,

        System.Text.Encoding.GetEncoding(response.CharacterSet));

    string result = readStream.ReadToEnd();
    response.Close();
    readStream.Close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文