Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将SOAP消息提交到外部Web服务URL?_C#_Visual Studio 2012_Soap Client - Fatal编程技术网

C# 如何将SOAP消息提交到外部Web服务URL?

C# 如何将SOAP消息提交到外部Web服务URL?,c#,visual-studio-2012,soap-client,C#,Visual Studio 2012,Soap Client,我正在尝试向外部Url发送SOAP消息。 我的代码如下所示 namespace SeadSOAPMessage { class Program { static void Main(string[] args) { CallWebService(); Console.ReadLine(); } public static void CallWebService()

我正在尝试向外部Url发送SOAP消息。 我的代码如下所示

namespace SeadSOAPMessage
{
    class Program
    {
        static void Main(string[] args)
        {
            CallWebService();
            Console.ReadLine();
        }


        public static void CallWebService()
        {
            var _url = "http://www.webservicex.com/globalweather.asmx";
            var _action = "http://www.webservicex.com/globalweather.asmx?op=GetWeather";


            XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
            HttpWebRequest webRequest = CreateWebRequest(_url, _action);
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

            // begin async call to web request.
            IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

            // suspend this thread until call is complete. You might want to
            // do something usefull here like update your UI.
            asyncResult.AsyncWaitHandle.WaitOne();

            // get the response from the completed web request.
            string soapResult;
            using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
            {
                using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                {
                    soapResult = rd.ReadToEnd();
                }
                Console.Write(soapResult);
            }
        }

        private static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

        private static XmlDocument CreateSoapEnvelope()
        {
            XmlDocument soapEnvelop = new XmlDocument();
            soapEnvelop.LoadXml(@"<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body><GetWeather xmlns=""http://www.webserviceX.NET""><CityName>string</CityName><CountryName>string</CountryName></GetWeather></soap:Body></soap:Envelope>");
            return soapEnvelop;
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }
    }
}
在那之后,如果我继续执行死刑,那么第二次我会得到另一个例外

EndGetResponse can only be called once for each asynchronous operation.

那么,我做错了什么?

通过在_action变量中传递一个正确的url来解决问题,即

 var _action = "http://www.webservicex.com/globalweather.asmx/GetWeather";
并且通过了错误的一个,即

var _action = "http://www.webservicex.com/globalweather.asmx?op=GetWeather";

通过在_action变量中传递正确的url解决了此问题,即

 var _action = "http://www.webservicex.com/globalweather.asmx/GetWeather";
并且通过了错误的一个,即

var _action = "http://www.webservicex.com/globalweather.asmx?op=GetWeather";

尝试在代理(如fiddler)中跟踪请求,查看响应中是否有任何额外信息。尝试在代理(如fiddler)中跟踪请求,查看响应中是否有任何额外信息。