• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#httpRequestSoap请求

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一般添加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>

  

 不构造XML结构请求也是可以的:

/*
             /*POST /WebSite1/Service.asmx/HelloWorld HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
             */
            //创建HttpWebRequest 实例,使用WebRequest.Create
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:1198/WebSite1/Service.asmx/HelloWorld");
            //发送请求
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength=0;
            string soap = "";//请求参数如 soap = "str=wgscd&num=123";
            //字符转字节
            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 (Exception ex)
            {
                txtResponse.Text = ex.Message ;
               
            }

  

会返回:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>

  

 不构造XML请求结构的一个死穴是,那种传人参数是某个对象/类的情况就GG了。

如:

[WebMethod]
public DataRet HelloWorld(DataRequest dt)
{

DataRet d =new DataRet();
return d;
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#并发集合并发集合发布时间:2022-07-13
下一篇:
C#中文和UNICODE编码转换C#中文和UNICODE编码转换发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap