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

2019-07-23 14:04发布

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>

3条回答
Melony?
2楼-- · 2019-07-23 14:29

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();
}
查看更多
够拽才男人
3楼-- · 2019-07-23 14:45

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

查看更多
趁早两清
4楼-- · 2019-07-23 14:49

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;
查看更多
登录 后发表回答