zoukankan      html  css  js  c++  java
  • C# HttpWebRequest 添加Cookie验证

    public static void Post3()
    {
    CookieContainer cookies = new CookieContainer();

    string RequestPara = "account=win&password=123";
    RequestPara = Regex.Replace(RequestPara, "%", "%25");
    byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);

    string url = "http:";
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    myHttpWebRequest.Method = "POST";
    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
    myHttpWebRequest.ContentLength = buf.Length;
    myHttpWebRequest.Timeout = 20 * 1000; //连接超时
    myHttpWebRequest.Accept = "*/*";
    myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
    myHttpWebRequest.CookieContainer = new CookieContainer(); //暂存到新实例

    System.IO.Stream RequestStream = myHttpWebRequest.GetRequestStream();
    RequestStream.Write(buf, 0, buf.Length);
    RequestStream.Close();

    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    cookies = myHttpWebRequest.CookieContainer; //保存cookies
    string cookiesstr = myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri); //把cookies转换成字符串

    Console.WriteLine(cookiesstr);

    //string data = "patient_name=测试"

    // + "&patient_gender="
    // + "&patient_sensibiligen="
    // + "&patient_address="
    // + "&patient_phone="
    // + "&idcard="
    // + "&doctor_rxnote_js="
    // + "&ans_id=" + ApplicationCOM.Uid
    // + "&patient_note="
    // + "&ds_sign_id=";
    //data = Regex.Replace(data, "%", "%25");
    //buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(data);

    url = "http:";
    myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    //myHttpWebRequest.Method = "POST";

    myHttpWebRequest.Method = "GET";

    myHttpWebRequest.ContentLength = buf.Length;
    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
    myHttpWebRequest.Timeout = 20 * 1000; //连接超时
    myHttpWebRequest.Accept = "*/*";
    myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
    myHttpWebRequest.Headers.Add("Cookie", cookiesstr);

    //RequestStream = myHttpWebRequest.GetRequestStream();
    //RequestStream.Write(buf, 0, buf.Length);
    //RequestStream.Close();

    myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();


    Stream stream = myHttpWebResponse.GetResponseStream();
    stream.ReadTimeout = 15 * 1000; //读取超时
    StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
    string strWebData = sr.ReadToEnd();
    Console.WriteLine(strWebData);
    }

    /// <summary>
    /// WebHTTP工具类
    /// </summary>

    public class WebHTTPUtil
    {
    private static string cookiesstr;
    /// <summary>
    /// 登陆
    /// </summary>
    /// <param name="account"></param>
    /// <param name="password"></param>
    public static void SignIn(string account, string password)
    {
    CookieContainer cookies = new CookieContainer();

    string RequestPara = "account=" + account + "&password=" + password;
    RequestPara = Regex.Replace(RequestPara, "%", "%25");
    byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);

    string url = "http";
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    myHttpWebRequest.Method = "POST";
    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
    myHttpWebRequest.ContentLength = buf.Length;
    myHttpWebRequest.Timeout = 20 * 1000; //连接超时
    myHttpWebRequest.Accept = "*/*";
    myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
    myHttpWebRequest.CookieContainer = new CookieContainer(); //暂存到新实例

    System.IO.Stream RequestStream = myHttpWebRequest.GetRequestStream();
    RequestStream.Write(buf, 0, buf.Length);
    RequestStream.Close();

    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    cookies = myHttpWebRequest.CookieContainer; //保存cookies
    cookiesstr = myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri); //把cookies转换成字符串

    }
    public static string POST(string url, string data)
    {
    data = Regex.Replace(data, "%", "%25");
    byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(data);

    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    myHttpWebRequest.Method = "POST";
    myHttpWebRequest.ContentLength = buf.Length;
    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
    myHttpWebRequest.Timeout = 20 * 1000; //连接超时
    myHttpWebRequest.Accept = "*/*";
    myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
    myHttpWebRequest.Headers.Add("Cookie", cookiesstr);

    System.IO.Stream RequestStream = myHttpWebRequest.GetRequestStream();
    RequestStream.Write(buf, 0, buf.Length);
    RequestStream.Close();

    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    Stream stream = myHttpWebResponse.GetResponseStream();
    stream.ReadTimeout = 15 * 1000; //读取超时
    StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
    string strWebData = sr.ReadToEnd();

    return strWebData;
    }
    public static string GET(string url)
    {
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    myHttpWebRequest.Method = "GET";
    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
    myHttpWebRequest.Timeout = 20 * 1000; //连接超时
    myHttpWebRequest.Accept = "*/*";
    myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
    myHttpWebRequest.Headers.Add("Cookie", cookiesstr);

    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    Stream stream = myHttpWebResponse.GetResponseStream();
    stream.ReadTimeout = 15 * 1000; //读取超时
    StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
    string strWebData = sr.ReadToEnd();
    return strWebData;
    }

    }

  • 相关阅读:
    事件处理之二:点击事件监听器的五种写法 分类: H1_ANDROID 2013-09-11 10:32 4262人阅读 评论(1) 收藏
    如何解决安卓SDK无法下载Package的问题 分类: H1_ANDROID 2013-09-09 10:26 1199人阅读 评论(0) 收藏
    adb常用命令 分类: H1_ANDROID 2013-09-08 15:22 510人阅读 评论(0) 收藏
    用IBM WebSphere DataStage进行数据整合: 第 1 部分 分类: H2_ORACLE 2013-08-23 11:20 688人阅读 评论(0) 收藏
    三大主流ETL工具选型 分类: H2_ORACLE 2013-08-23 11:17 426人阅读 评论(0) 收藏
    ETL概述 分类: H2_ORACLE 2013-08-23 10:36 344人阅读 评论(0) 收藏
    POI操作Excel常用方法总结 分类: B1_JAVA 2013-08-23 10:01 349人阅读 评论(0) 收藏
    段的创建表user_segments 分类: H2_ORACLE 2013-08-10 11:13 714人阅读 评论(0) 收藏
    让android项目支持boost 支持c++11
    unity中全屏背景图缩放
  • 原文地址:https://www.cnblogs.com/-jingzhe/p/13747894.html
Copyright © 2011-2022 走看看