0

我在这里写的是关于使用 HTTPWebRequest for Windows Phone 应用程序使用肥皂服务的查询。我正在按照以下步骤构建请求并传递给服务器。但我无法连接到此服务器并返回“远程服务器返回错误:未找到”。但我可以很好地使用 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();           
}
4

0 回答 0