2017-07-21 117 views
-2

我正在尋找在c#中創建soap消息, 這裏我創建了一個客戶端,使用命令提示符下的命令現在嘗試創建一個soap消息,但我對這個概念非常陌生,所以無法找到正確的方法任何人對此有何想法?如何在c#中創建SOAP消息?

+0

你跑什麼命令來創建客戶端?你能分享客戶代碼嗎?您爲編寫SOAP消息編寫了哪些代碼? –

+0

你在傳遞什麼,你在哪裏傳遞,爲什麼傳遞,你在犯什麼錯誤,我們無法知道沒有看到你的代碼。因此,如果您需要我們提供最適合您的問題的解決方案,請分享代碼。 –

回答

1

示例代碼來創建SOAP請求

using System; 
using System.IO; 
using System.Net; 
using System.Xml; 

namespace UsingSOAPRequest 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
      //creating object of program class to access methods 
      Program obj = new Program(); 
      Console.WriteLine("Please Enter Input values.."); 
      //Reading input values from console 
      int a = Convert.ToInt32(Console.ReadLine()); 
      int b = Convert.ToInt32(Console.ReadLine()); 
      //Calling InvokeService method 
      obj.InvokeService(a, b); 
     } 
     public void InvokeService(int a, int b) 
     { 
      //Calling CreateSOAPWebRequest method 
      HttpWebRequest request = CreateSOAPWebRequest(); 

      XmlDocument SOAPReqBody = new XmlDocument(); 
      //SOAP Body Request 
      SOAPReqBody.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?> 
      <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema- instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> 
      <soap:Body> 
       <Addition xmlns=""http://tempuri.org/""> 
        <a>" + a + @"</a> 
        <b>" + b + @"</b> 
       </Addition> 
       </soap:Body> 
      </soap:Envelope>"); 


      using (Stream stream = request.GetRequestStream()) 
      { 
       SOAPReqBody.Save(stream); 
      } 
      //Geting response from request 
      using (WebResponse Serviceres = request.GetResponse()) 
      { 
       using (StreamReader rd = new StreamReader(Serviceres.GetResponseStream())) 
       { 
        //reading stream 
        var ServiceResult = rd.ReadToEnd(); 
        //writting stream result on console 
        Console.WriteLine(ServiceResult); 
        Console.ReadLine(); 
       } 
      } 
     } 

     public HttpWebRequest CreateSOAPWebRequest() 
     { 
      //Making Web Request 
      HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"http://localhost/Employee.asmx"); 
      //SOAPAction 
      Req.Headers.Add(@"SOAPAction:http://tempuri.org/Addition"); 
      //Content_type 
      Req.ContentType = "text/xml;charset=\"utf-8\""; 
      Req.Accept = "text/xml"; 
      //HTTP method 
      Req.Method = "POST"; 
      //return HttpWebRequest 
      return Req; 
     } 
    } 
} 
+0

我需要在.config文件中添加任何東西嗎? – Madhav