.NET에서 SOAP 요청을 게시하는 방법?


Answers

나는 XML 요청을 수동으로 작성한 다음 webrequest 객체를 사용하여 요청을 제출한다.

string data = "the xml document to submit";
string url = "the webservice url";
string response = "the response from the server";

// build request objects to pass the data/xml to the server
byte[] buffer = Encoding.ASCII.GetBytes(data);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
Stream post = request.GetRequestStream();

// post data and close connection
post.Write(buffer, 0, buffer.Length);
post.Close();

// build response object
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responsedata = response.GetResponseStream();
StreamReader responsereader = new StreamReader(responsedata);
response = responsereader.ReadToEnd();

코드의 시작 부분에있는 문자열 변수는 사용자가 설정 한 것입니다. 그런 다음 서버에서 문자열 응답을받습니다 (바라건대 ...).

Question

XML 파일에 SOAP 요청이 있습니다. .net에서 웹 서비스에 요청을 게시하고 싶습니다. 구현하는 방법은 무엇입니까?




오래된 스레드를 부딪쳐서 죄송합니다. 여기에 내 해결책이 있습니다.

''' <summary>
''' Sends SOAP to a web service and sends back the XML it got back.
''' </summary>
Public Class SoapDispenser
    Public Shared Function CallWebService(ByVal WebserviceURL As String, ByVal SOAP As String) As XmlDocument
        Using wc As New WebClient()
            Dim retXMLDoc As New XmlDocument()

            wc.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8")
            retXMLDoc.LoadXml(wc.UploadString(WebserviceURL, SOAP))

            Return retXMLDoc
        End Using
    End Function
End Class



다음은 또 다른 예입니다 - VB에서이 예가 있습니다.

    Dim manualWebClient As New System.Net.WebClient()

    manualWebClient.Headers.Add("Content-Type", "application/soap+xml;  charset=utf-8")

    ' Note: don't put the <?xml... tag in--otherwise it will blow up with a 500 internal error message!
    Dim bytArguments As Byte() = System.Text.Encoding.ASCII.GetBytes( _
        "<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">" & System.Environment.NewLine & _
        "  <soap12:Body>" & System.Environment.NewLine & _
        "    <Multiply xmlns=""http://cptr446.class/"">" & System.Environment.NewLine & _
        "      <x>5</x>" & System.Environment.NewLine & _
        "      <y>4</y>" & System.Environment.NewLine & _
        "    </Multiply>" & System.Environment.NewLine & _
        "  </soap12:Body>" & System.Environment.NewLine & _
        "</soap12:Envelope>")
    Dim bytRetData As Byte() = manualWebClient.UploadData("http://localhost/CPTR446.asmx", "POST", bytArguments)

    MessageBox.Show(System.Text.Encoding.ASCII.GetString(bytRetData))