zoukankan      html  css  js  c++  java
  • WebRequest 对象的使用(转)

    WebRequest 对象的使用

    http://www.cnblogs.com/haogj/archive/2011/06/09/2076708.html

    (使用的是控制台程序,注释讲的比较清楚) 

    内容比较简单,直接看代码的注释即可。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    // 待请求的地址
    string url = "http://www.cnblogs.com";
                 
    // 创建 WebRequest 对象,WebRequest 是抽象类,定义了请求的规定,
    // 可以用于各种请求,例如:Http, Ftp 等等。
    // HttpWebRequest 是 WebRequest 的派生类,专门用于 Http
    System.Net.HttpWebRequest request
        = System.Net.HttpWebRequest.Create(url) as System.Net.HttpWebRequest;
     
    // 请求的方式通过 Method 属性设置 ,默认为 GET
    // 可以将 Method 属性设置为任何 HTTP 1.1 协议谓词:GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。
    request.Method = "POST";
     
    // 还可以在请求中附带 Cookie
    // 但是,必须首先创建 Cookie 容器
    request.CookieContainer = new System.Net.CookieContainer();
     
    System.Net.Cookie requestCookie
        = new System.Net.Cookie("Request", "RequestValue","/", "localhost");
    request.CookieContainer.Add(requestCookie);
     
    Console.WriteLine("请输入请求参数:");
     
    // 输入 POST 的数据.
    string inputData = Console.ReadLine();
     
    // 拼接成请求参数串,并进行编码,成为字节
    string postData = "firstone=" + inputData;
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] byte1 = encoding.GetBytes(postData);
     
    // 设置请求的参数形式
    request.ContentType = "application/x-www-form-urlencoded";
     
    // 设置请求参数的长度.
    request.ContentLength = byte1.Length;
     
    // 取得发向服务器的流
    System.IO.Stream newStream = request.GetRequestStream();
     
    // 使用 POST 方法请求的时候,实际的参数通过请求的 Body 部分以流的形式传送
    newStream.Write(byte1, 0, byte1.Length);
     
    // 完成后,关闭请求流.
    newStream.Close();
     
    // GetResponse 方法才真的发送请求,等待服务器返回
    System.Net.HttpWebResponse response
        = (System.Net.HttpWebResponse)request.GetResponse();
     
    // 首先得到回应的头部,可以知道返回内容的长度或者类型
    Console.WriteLine("Content length is {0}", response.ContentLength);
    Console.WriteLine("Content type is {0}", response.ContentType);
     
    // 回应的 Cookie 在 Cookie 容器中
    foreach (System.Net.Cookie cookie in response.Cookies)
    {
        Console.WriteLine("Name: {0}, Value: {1}", cookie.Name, cookie.Value);
    }
    Console.WriteLine();
     
    // 然后可以得到以流的形式表示的回应内容
    System.IO.Stream receiveStream
        = response.GetResponseStream();    
     
    // 还可以将字节流包装为高级的字符流,以便于读取文本内容
    // 需要注意编码
    System.IO.StreamReader readStream
        = new System.IO.StreamReader(receiveStream, Encoding.UTF8);
     
    Console.WriteLine("Response stream received.");
    Console.WriteLine(readStream.ReadToEnd());
     
    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
    response.Close();
    readStream.Close();
     
  • 相关阅读:
    只允许在input框输入文字,不能输入数字和其他字符
    阻止用户在input框输入数字
    centos 7.2安装和配置MongoDB
    Python基础
    Python小练习008
    Python小练习007
    Python小练习006
    Python错误集锦
    Python和MongoDB
    MongoDB笔记
  • 原文地址:https://www.cnblogs.com/gongyu/p/3977166.html
Copyright © 2011-2022 走看看