How to call a web service with no wsdl in .net

后端 未结 5 569
旧巷少年郎
旧巷少年郎 2020-12-08 04:53

I have to connect to a third party web service that provides no wsdl nor asmx. The url of the service is just http://server/service.soap

I have read this article ab

5条回答
  •  感动是毒
    2020-12-08 05:42

    I have created the following helper method to call WebService manually without any reference:

    public static HttpStatusCode CallWebService(string webWebServiceUrl, 
                                                string webServiceNamespace, 
                                                string methodName, 
                                                Dictionary parameters, 
                                                out string responseText)
    {
        const string soapTemplate = 
    @"
    
      
        <{0} xmlns=""{1}"">
          {2}    
      
    ";
    
        var req = (HttpWebRequest)WebRequest.Create(webWebServiceUrl);
        req.ContentType = "application/soap+xml;";
        req.Method = "POST";
    
        string parametersText;
    
        if (parameters != null && parameters.Count > 0)
        {
            var sb = new StringBuilder();
            foreach (var oneParameter in parameters)
                sb.AppendFormat("  <{0}>{1}\r\n", oneParameter.Key, oneParameter.Value);
    
            parametersText = sb.ToString();
        }
        else
        {
            parametersText = "";
        }
    
        string soapText = string.Format(soapTemplate, methodName, webServiceNamespace, parametersText);
    
    
        using (Stream stm = req.GetRequestStream())
        {
            using (var stmw = new StreamWriter(stm))
            {
                stmw.Write(soapText);
            }
        }
    
        var responseHttpStatusCode = HttpStatusCode.Unused;
        responseText = null;
    
        using (var response = (HttpWebResponse)req.GetResponse())
        {
            responseHttpStatusCode = response.StatusCode;
    
            if (responseHttpStatusCode == HttpStatusCode.OK)
            {
                int contentLength = (int)response.ContentLength;
    
                if (contentLength > 0)
                {
                    int readBytes = 0;
                    int bytesToRead = contentLength;
                    byte[] resultBytes = new byte[contentLength];
    
                    using (var responseStream = response.GetResponseStream())
                    {
                        while (bytesToRead > 0)
                        {
                            // Read may return anything from 0 to 10. 
                            int actualBytesRead = responseStream.Read(resultBytes, readBytes, bytesToRead);
    
                            // The end of the file is reached. 
                            if (actualBytesRead == 0)
                                break;
    
                            readBytes += actualBytesRead;
                            bytesToRead -= actualBytesRead;
                        }
    
                        responseText = Encoding.UTF8.GetString(resultBytes);
                        //responseText = Encoding.ASCII.GetString(resultBytes);
                    }
                }
            }
        }
    
        // standard responseText: 
        //
        //
        //    
        //        
        //            Hello
        //        
        //    
        //
        if (!string.IsNullOrEmpty(responseText))
        {
            string responseElement = methodName + "Result>";
            int pos1 = responseText.IndexOf(responseElement);
    
            if (pos1 >= 0)
            {
                pos1 += responseElement.Length;
                int pos2 = responseText.IndexOf(" pos1)
                    responseText = responseText.Substring(pos1, pos2 - pos1);
            }
            else
            {
                responseText = ""; // No result
            }
        }
    
        return responseHttpStatusCode;
    }
    

    You can then simply call any web service method with the following code:

    var parameters = new Dictionary();
    parameters.Add("name", "My Name Here");
    
    string responseText;
    var responseStatusCode = CallWebService("http://localhost/TestWebService.asmx", 
                                            "http://tempuri.org/", 
                                            "SayHello", 
                                            parameters, 
                                            out responseText);
    

提交回复
热议问题