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>的内容为返回的响应文本内容。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在进行HTTP请求时,若请求的目标接口使用了HTTPS协议,则需要进行HTTPS连接。HTTPS连接的安全性要比HTTP连接高,因为它使用了SSL/TLS协议对请求和响应进行加密。在进行HTTPS连接时,需要完成以下步骤: 1. 首先,需要将HTTPS地址解析为IP地址和端口号。 2. 在完成地址解析后,需要使用Socket准备好与HTTPS服务器建立连接的通道。 3. 建立连接后,发送一份HTTP请求头给HTTPS服务器,请求需要包含请求的方式、路径、协议版本和发送的数据。 4. 发送请求头后,HTTPS服务器会发送回一份HTTP响应头,响应头需要包含响应的状态码、协议版本和响应数据的长度等信息。 5. 根据HTTP响应头的状态码来判断请求是否成功,如果成功,则可以继续读取响应体的数据。 6. 在读取响应体的数据时,需要注意数据是否已经全部到达,否则会产生数据被截断的情况。 7. 在读取完响应体后,需要关闭与HTTPS服务器的连接。 总之,在进行HTTP请求时,对于使用HTTPS协议的接口,需要对请求进行额外的处理保证安全连接性。 ### 回答2: 首先,了解HTTP请求是什么。HTTP请求是指客户端向服务端发送请求,然后服务端返回响应。HTTP请求通常包含请求方法、URL、请求头部、请求体等组成部分。 接下来,需要了解HTTPS接口。HTTPS是一种加密的HTTP请求,安全性更高。HTTPS使用SSL协议实现数据加密,保证了数据的安全性和完整性。 当我们使用HTTP请求HTTPS接口时,我们需要遵循以下步骤: 1.建立SSL连接:客户端需要向服务端发送握手请求,进行SSL协议的版本协商、加密算法协商等,然后将握手信息进行加密发送给服务端。 2.服务端响应握手:服务端收到请求后进行响应,返回SSL协议版本、加密算法等信息。 3.证书验证:客户端会对服务端返回的证书进行验证,以确保它是由受信任的CA颁发的有效证书。 4.建立安全通道:一旦证书验证通过,客户端和服务端就会建立安全通道,开始进行加密通信。 5.发送HTTP请求:在安全通道建立完毕后,客户端就可以发送HTTP请求了。请求内容也是经过加密的。 6.服务端响应:服务端接收到加密的HTTP请求后,进行解密操作,然后返回加密的HTTP响应,客户端也进行解密操作后得到明文响应。 总之,HTTP请求HTTPS接口需要经过SSL握手和证书验证等多个步骤,确保通信的安全性。我们需要使用相应的工具,如HttpClient等来实现。同时,我们还需要对服务器端的证书进行验证,以减少安全风险。 ### 回答3: HttpRequest是用于网页发送HTTP请求的类,而HTTPS是指在HTTP协议下加入SSL层,使传输过程的数据被加密保护,提高了数据的安全性。 当我们需要通过网络通信获取数据时,常用HttpRequest请求接口实现数据返回。当需要访问HTTPS接口时,可以使用以下方法: 1. 在HttpRequest请求对象设置访问的URL:使用HTTPS开头的URL地址 HttpRequest request = new HttpRequest("https://www.example.com/api/get_data"); 2. 设置HTTPS的信任证书:由于HTTPS为加密协议,过程涉及证书的验证。一般情况下是从对方网站获得的证书,也可以通过第三方认证机构获得。在程序层面,需要为访问HTTPS服务设置信任证书 Security.setProperty("ssl.SocketFactory.provider", "com.sun.net.ssl.internal.ssl.Provider"); System.setProperty("javax.net.ssl.trustStoreType", "jks"); System.setProperty("javax.net.ssl.trustStore", "truststore.jks"); 3. 执行HttpRequest请求并解析返回数据 HttpResponse response = request.getResponse(); String content = response.getContent(); 这样,当我们发送包含HTTPS协议的接口请求时,便可以得到返回结果了。需要注意的是,在请求HTTPS接口的过程,网络通信的数据是被加密的,因此访问效率可能会有所降低。这时,我们需要权衡数据安全性和访问效率,选择适合自己的方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值