Sending SOAP message with C# Help Needed

后端 未结 4 1188
我寻月下人不归
我寻月下人不归 2020-12-16 07:00

I would like to send a SOAP Message to a Web Service and read the response. My code is as follows: I will appreciate your help.

I hope my question is not repeate

4条回答
  •  难免孤独
    2020-12-16 07:53

    You can access the service in 2 methods:

    1. By adding a web reference of the service. In visual studio you can right click on your project and select the option Add Web reference and then paste the URL of the service.

    2. Generate client proxy from the wsdl using wsdl tool from Visual studio command prompt.The command would be as follows:

    c:>wsdl "http://coreg.surveycenter.com/RegistrationGateway/PanelistService.asmx?wsdl

    It would generate a .cs file and an output.config. Include the .cs file in your project and you can directly use it to access the service. Make sure that the config file entries are added to your projects config.

    If you want to use HttpWebRequest then find the code below :

    string soap = 
    @"
    
      
        
          123
          string
        
      
    ";
    
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/WebServices/CustomerWebService.asmx");
    req.Headers.Add("SOAPAction", "\"http://tempuri.org/Register\"");
    req.ContentType = "text/xml;charset=\"utf-8\"";
    req.Accept = "text/xml";
    req.Method = "POST";
    
    using (Stream stm = req.GetRequestStream())
    {
         using (StreamWriter stmw = new StreamWriter(stm))
         {
              stmw.Write(soap);
         }
    }
    
    WebResponse response = req.GetResponse();
    
    Stream responseStream = response.GetResponseStream();
    // TODO: Do whatever you need with the response
    

    My Service looks like :

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class CustomerWebService : System.Web.Services.WebService
    {
        [WebMethod]
        public string Register(long id, string data1)
        {
            return "ID.CUSTOMER";
        }
    }
    

提交回复
热议问题