private static string WebServiceCall(string methodName)
{
WebRequest webRequest = WebRequest.Create("http://localhost/AccountSvc/DataInquiry.asmx");
HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml; charset=utf-8";
httpRequest.Headers.Add("SOAPAction: http://tempuri.org/" + methodName);
httpRequest.ProtocolVersion = HttpVersion.Version11;
httpRequest.Credentials = CredentialCache.DefaultCredentials;
Stream requestStream = httpRequest.GetRequestStream();
//Create Stream and Complete Request
StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
StringBuilder soapRequest = new StringBuilder("<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"");
soapRequest.Append(" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ");
soapRequest.Append("xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>");
soapRequest.Append("<GetMyName xmlns="http://tempuri.org/"><name>Sam</name></GetMyName>");
soapRequest.Append("</soap:Body></soap:Envelope>");
streamWriter.Write(soapRequest.ToString());
streamWriter.Close();
//Get the Response
HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
StreamReader srd = new StreamReader(wr.GetResponseStream());
string resulXmlFromWebService = srd.ReadToEnd();
return resulXmlFromWebService;
}
I tried different code to send/receive soap responses but all fail with the same "The remote server returned an error: (500) Internal Server Error."
.
I can access the same service using SoapUI. Am able to invoke the method too. I read in this forum that the reason why am I getting 500 error could be wrong header. I verified the header, it seems to be ok. I would appreciate if someone can help.
Following is the sample SOAP request:
POST /AccountSvc/DataInquiry.asmx HTTP/1.1
Host: abc.def.gh.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetMyName"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetMyName xmlns="http://tempuri.org/">
<name>string</name>
</GetMyName>
</soap:Body>
</soap:Envelope>
I used the above sample request to execute the method and it worked. Here is the Soap request that I passed:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GetMyName xmlns="http://tempuri.org/"><name>Sam</name></GetMyName></soap:Body></soap:Envelope>
Edit:
I have updated the code above in WebServiceCall that worked for .asmx service. But the same code didn't work for WCF service. Why?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…