How to Call HTTP based Java Web Service(Servlet) i

2019-07-23 14:10发布

问题:

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