0

ここでは、Windows Phone アプリケーションの HTTPWebRequest で 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();           
}
4

0 に答える 0