C# HttpRequest 请求WebService接口

WebService接口如下图所示:

 1.上部分xml文档内容为需要发送的报文:

<?xml version="1.0" encoding="utf-8"?>
<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>
    <HelloWorldxml xmlns="http://tempuri.org/">
      <xmlName>string</xmlName>
      <xmlDoc>xml</xmlDoc>
    </HelloWorldxml>
  </soap:Body>
</soap:Envelope>

<HelloWorldxml xmlns="http://tempuri.org/"> 中的HelloWorldxml,实际上是该webService要发送请求的函数名称。
如下图所示:

生成该xml内容的代码如下: 

private string CreateXmlData1()
{
    XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
    XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
    XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
    XNamespace xmlns = "http://tempuri.org/";

    XmlDocument XDocData = new XmlDocument();
    XmlDocument pXmlDocument = new XmlDocument();
    XmlDeclaration pXmlDeclaration = pXmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
    pXmlDocument.AppendChild(pXmlDeclaration);
    XmlElement root = pXmlDocument.CreateElement("soap", "Envelope", "urn:soap");//加入根节点 
    root.SetAttribute("xmlns:xsi", xsi.ToString());//xml的名称空间
    root.SetAttribute("xmlns:xsd", xsd.ToString());//xml的名称空间
    root.SetAttribute("xmlns:soap", soap.ToString());//xml的名称空间
    pXmlDocument.AppendChild(root);

    XmlElement body = pXmlDocument.CreateElement("soap", "Body", "urn:soap");//
    root.AppendChild(body);

    XmlElement LOT_COLECTDATA = pXmlDocument.CreateElement("HelloWorldxml");//
    LOT_COLECTDATA.SetAttribute("xmlns", xmlns.ToString());
    body.AppendChild(LOT_COLECTDATA);

    XmlElement msg = pXmlDocument.CreateElement("xmlName");//
    msg.InnerText = "xml名称";
    LOT_COLECTDATA.AppendChild(msg);

    XmlElement DATA = pXmlDocument.CreateElement("DATA");
    //DATA.InnerText = "Hello";
    LOT_COLECTDATA.AppendChild(DATA);

    XmlElement ITEMID = pXmlDocument.CreateElement("ITEMID");//
    ITEMID.InnerText = "itemName";
    DATA.AppendChild(ITEMID);

    XmlElement UNIT = pXmlDocument.CreateElement("UNIT");//
    UNIT.InnerText = "unitdata";
    DATA.AppendChild(UNIT);

    XmlElement RESULT = pXmlDocument.CreateElement("RESULT");//
    RESULT.InnerText = "result";
    DATA.AppendChild(RESULT);

    return pXmlDocument.InnerXml;
}

发送HttpRequest请求的代码如下:

public static string GetService1(string url, string par)
{
    string res = "";
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse webResponse = null;
    Stream writer = null;
    Stream reader = null;
    byte[] data = Encoding.UTF8.GetBytes(par);
    webRequest.Method = "POST";
    webRequest.ContentType = "text/xml; charset=utf-8";
    webRequest.ContentLength = data.Length;

    //写入参数
    try
    {
        writer = webRequest.GetRequestStream();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    writer.Write(data, 0, data.Length);
    writer.Close();

    //获取响应
    try
    {
        webResponse = (HttpWebResponse)webRequest.GetResponse();
        reader = webResponse.GetResponseStream();
    }
    catch (Exception ex)
    {

    }
    StreamReader streamReader = new StreamReader(reader, Encoding.UTF8);
    res = streamReader.ReadToEnd();
    reader.Close();
    streamReader.Close();
    XmlDocument document = new XmlDocument();
    document.LoadXml(res);
    res = document.GetElementsByTagName("HelloWorldxmlResult").Item(0).InnerText;
    return res;
}

调用并执行:

private void button1_Click(object sender, EventArgs e)
{
    string par = CreateXmlData().Replace("soap:Body xmlns:soap=\"urn:soap\"", "soap:Body");
    this.textBox2.Text = par;
    string result = GetService1(this.textBox1.Text, par);
    this.label1.Text = "返回的结果是:" + result;
}


2.下部分的XML文档为请求过后webService返回的响应内容:
 

<?xml version="1.0" encoding="utf-8"?>
<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>
    <HelloWorldxmlResponse xmlns="http://tempuri.org/">
      <HelloWorldxmlResult>string</HelloWorldxmlResult>
    </HelloWorldxmlResponse>
  </soap:Body>
</soap:Envelope>

其中<HelloWorldxmlResult>string</HelloWorldxmlResult>的内容为返回的响应文本内容。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值