我在这里写的是使用HTTPWebRequest for Windows Phone应用程序使用soap服务的查询。我按照以下步骤构建请求并传递给服务器。但是我无法连接此服务器并返回“远程服务器返回错误:NotFound”。但我使用Fiddler工具可以正常工作。
第1步
- >使用所需标头创建请求。 (请问您能否以正确的方式验证“SOAPAction”标题。)
public void HttpSoapPost(string uri, AsyncCallback callBack)
{
try
{
ServerURL = uri;
uri = uri + "/Reports1.1/WS/Retrieve.svc/soap";
// uri value will be "https://onsite.soaptestingserver.com/Reports1.1/WS/Retrieve.svc/soap
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = Common.Method;
request.ContentType = "text/xml; charset=utf-8";
request.Headers[HttpRequestHeader.ContentLength] = "297";
request.Headers[HttpRequestHeader.UserAgent] = "WP7";
request.Headers[HttpRequestHeader.Connection] = Common.Connection;
request.Headers[HttpRequestHeader.AcceptEncoding] = Common.AcceptEncoding;
request.Headers[HttpRequestHeader.Host] = "soaptestingserver.com:8082";
request.Headers[HttpRequestHeader.Expect] = "100-continue";
request.Headers["SOAPAction"] = "https://onsite.soaptestingserver.com/Reports1.1/WS/Retrieve.svc/IRetrieve/GetVersions";
request.BeginGetResponse(callBack, request);
}
catch (Exception exp)
{
throw exp;
}
}
步骤2:发送添加正文内容的请求。
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
//StreamWriter streamWriter = new StreamWriter(postStream, Encoding.UTF8);
StringBuilder soapRequest = new StringBuilder("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
soapRequest.Append("<s:Body>");
soapRequest.Append("<GetVersions xmlns=\"https://www.onsite.soaptestingserver.com/Reports1.1/WS/Retrieve.svc\">");
soapRequest.Append("<Request xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">");
soapRequest.Append("<Language>en-EN</Language>");
soapRequest.Append("</Request>");
soapRequest.Append("</GetVersions></s:Body></s:Envelope>");
string postData = soapRequest.ToString();
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Write to the request stream.
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation /* I'm getting the exception in the next line as "The remote server returned an error: NotFound."*/
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
Console.WriteLine(responseString);
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
}