zoukankan      html  css  js  c++  java
  • HttpWebRequest 发送简单参数

    1、发送端

    using System.Net;
    using System.Text;
    using System.IO;

    // Prepare web request
    //string url="http://localhost:5814/WebSite1/Default2.aspx"
    string url = "http://localhost:10209/10、传递参数/httpWebRequest2.aspx";

    // 创建 WebRequest 对象,WebRequest 是抽象类,定义了请求的规定,
    // 可以用于各种请求,例如:Http, Ftp 等等。
    // HttpWebRequest 是 WebRequest 的派生类,专门用于 Http
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);

    // 请求的方式通过 Method 属性设置 ,默认为 GET
    // 可以将 Method 属性设置为任何 HTTP 1.1 协议谓词:GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。
    myRequest.Method = "POST";


    // 拼接成请求参数串,并进行编码,成为字节
    string strId = "jaer";
    string strPassword = "123456";
    ASCIIEncoding encoding = new ASCIIEncoding();
    string postData = "userid=" + strId;
    postData += "&password=" + strPassword;

    byte[] data = encoding.GetBytes(postData);

    // 设置请求的参数形式
    myRequest.ContentType = "application/x-www-form-urlencoded";
    // 设置请求参数的长度.
    myRequest.ContentLength = data.Length;


    // 取得发向服务器的流
    Stream newStream = myRequest.GetRequestStream();

    // Send the data.
    // 取得发向服务器的流
    newStream.Write(data, 0, data.Length);
    // 完成后,关闭请求流.
    newStream.Close();


    // Get response
    // GetResponse 方法才真的发送请求,等待服务器返回
    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

    // 然后可以得到以流的形式表示的回应内容
    Stream receiveStream=myResponse.GetResponseStream();

    // 还可以将字节流包装为高级的字符流,以便于读取文本内容
    // 需要注意编码
    StreamReader reader = new StreamReader(receiveStream, Encoding.Default);
    string content = reader.ReadToEnd();
    Response.Write(content);

    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
    myResponse.Close();
    receiveStream.Close();

    2、 接收端: 

    string ID = Request.Form["userid"].ToString();
    string password = Request.Form["password"].ToString();
    if (ID == "jaer" && password == "123456")
    {
    Response.Write(200);
    //Response.Write("啊啊啊");
    }

    说明: 发送的是两个参数,没有过多的处理。

    对于发送端的各种过冲介绍的比较清楚。

    WebRequest 使用的基本思路:

    (1) WebRequest 是即一个虚类,使用的时候要具体实例化,分为http对象和ftp对象,这是两种传输格式。

    (2)HttpWebRequest 具体传送的是二进制流,所以要对传输的信息进行编码、转换

               ASCIIEncoding encoding = new ASCIIEncoding();  ---编码方式

               byte[] data = encoding.GetBytes(postData);  --字符串编码为数组

               Stream newStream = myRequest.GetRequestStream();--设定流

               newStream.Write(data, 0, data.Length);)--- 将数据填充到流

        为了能够正确的解析二进制流,流头部具有规定的格式说明:

    (3)设定好了,把流发出去就可以了。

         // Send the data.
           // 取得发向服务器的流
            newStream.Write(data, 0, data.Length);
          // 完成后,关闭请求流.
           newStream.Close();

       // Get response
           // GetResponse 方法才真的发送请求,等待服务器返回
           HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

        疑问:到底是哪个发送的?没有发送之前就能把流给关闭了?流发送的,但是只是填写了流的内容,流就调用HttpWebRequest进行发送了,不能啊????

  • 相关阅读:
    struts.xml 配置
    result重定向到一个action
    Action类中通过ServlexxxAware接口的方式来获取来获取web资源
    Action类中通过ServletActionContext来获取web资源
    Action类中通过继承xxxAware接口来获取web资源
    Action类中通过ActionContext来获取web资源
    java中日期格式转换
    java类的执行顺序
    批量删除Redis数据库中的Key
    Python等同于PHP的 strip_tags?
  • 原文地址:https://www.cnblogs.com/gongyu/p/3977345.html
Copyright © 2011-2022 走看看