zoukankan      html  css  js  c++  java
  • 运行时创建HTTP请求及请求的处理

    1、发起请求

    下面这个方法的作用就是接收要发送的数据及数据要发送到的URL,然后返回响应数据

        protected string SendRequest(string data,string url)
    {
    WebRequest req
    = WebRequest.Create(url);
    req.Method
    = "POST";
    req.ContentType
    = "application/x-www-form-urlencoded";
    byte[] sendBytes = Encoding.UTF8.GetBytes(data);
    req.ContentLength
    = sendBytes.Length;
    Stream reqStream
    = req.GetRequestStream();
    reqStream.Write(sendBytes,
    0, sendBytes.Length);
    reqStream.Close();

    WebResponse res
    = req.GetResponse();
    Stream resStream
    = res.GetResponseStream();
    StreamReader sr
    = new StreamReader(resStream, Encoding.UTF8);
    string resData = sr.ReadToEnd();
    sr.Close();
    resStream.Close();
    return resData;
    }
    
    

    使用示例:

        protected void btnSubscribe_Click(object sender, EventArgs e)
    {
    string FileName = Server.MapPath("订购.xml");
    FileStream fs
    = new FileStream(FileName, FileMode.Open);
    StreamReader sr
    = new StreamReader(fs);
    string ReqData = sr.ReadToEnd();
    sr.Close();
    fs.Close();
    txtResponse.Text
    = SendRequest(ReqData, "http://localhost:2102/InterWeb/subscribe.htm");
    }
    
    

    2、处理请求
      接收要简单一些,用下面这两句代码就可以了

                StreamReader sr = new StreamReader(Request.InputStream);
    ReqData
    = sr.ReadToEnd();
    
    
    

    3、总结

    这种处理方式一般会用在一些对外的接口等的开发中,以XML传递协议数据。

  • 相关阅读:
    How To Install MySQL on Ubuntu 16.04
    Rabbitmq vs. kafka
    Expanded encryption and decryption signature algorithm SM2 & SM3
    Open Source CRM
    在WIN10打造成能运行Oracle的JDK的Linux
    Spring Cloud Zipkin
    Debian中APT的前世今生
    nginx for Windows Known issues:path
    Ajax cross domain
    JQuery Cross Domain Ajax(jsonp)
  • 原文地址:https://www.cnblogs.com/Aricc/p/1337319.html
Copyright © 2011-2022 走看看