C# httpRequest Soap请求

一般添加web服务引用是.NET用代理类模式 创建SOAP请求代理类,代理类是.NET开发工具VS自动给你生成。

下面用一般HTTP的模式有时候可能更合适,原理是构造SOAP请求的XML后POST过去:

下面是HelloWorld的例子

private void button1_Click(object sender, EventArgs e)
        {

            //创建HttpWebRequest 实例,使用WebRequest.Create
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:1198/WebSite1/Service.asmx");
            //发送请求
            webRequest.Method = "POST";
            //编码
            webRequest.ContentType = "text/xml";
            string soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            soap += "<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 += " <soap:Header>";
            soap += " </soap:Header>";
            soap += "<soap:Body>";
            soap += "  <HelloWorld xmlns=\"http://tempuri.org/\" />";
            soap += "</soap:Body>";
            soap += "</soap:Envelope>";
          // webRequest.Headers["SoapAction"] = "http://localhost:1198/WebSite1/Service.asmx";

            //字符转字节
            byte[] bytes = Encoding.UTF8.GetBytes(soap);
            Stream writer = webRequest.GetRequestStream();
            writer.Write(bytes, 0, bytes.Length);
            writer.Flush();
            writer.Close();
            string result = "";
            //返回 HttpWebResponse
            try
            {
                HttpWebResponse hwRes = webRequest.GetResponse() as HttpWebResponse;
                if (hwRes.StatusCode == System.Net.HttpStatusCode.OK)
                {//是否返回成功
                    Stream rStream = hwRes.GetResponseStream();
                    //流读取
                    StreamReader sr = new StreamReader(rStream, Encoding.UTF8);
                    result = sr.ReadToEnd();
                    sr.Close();
                    rStream.Close();
                }
                else
                {
                    result = "连接错误";
                }
                //关闭
                hwRes.Close();
                txtResponse.Text = result;
            }
            catch (System.Net.WebException ex)
            {
                String responseFromServer = ex.Message.ToString() + " ";
                if (ex.Response != null)
                {
                    using (WebResponse response = ex.Response)
                    {
                        Stream data = response.GetResponseStream();
                        using (StreamReader reader = new StreamReader(data))
                        {
                            responseFromServer += reader.ReadToEnd();
                        }
                    }
                }
                txtResponse.Text = responseFromServer;
            }


        }

  

不错意外会返回如下:

<?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><HelloWorldResponse xmlns="http://tempuri.org/"><HelloWorldResult>Hello World</HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>

  

猜你喜欢

转载自www.cnblogs.com/wgscd/p/10097402.html
今日推荐