How to Call HTTP based Java Web Service(Servlet) in Asp.Net.

久未见 提交于 2019-12-24 11:13:22

问题


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>

回答1:


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




回答2:


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;



回答3:


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();
}


来源:https://stackoverflow.com/questions/7874943/how-to-call-http-based-java-web-serviceservlet-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!