zoukankan      html  css  js  c++  java
  • C# HTTP Get Post 提交数据可以指定代理IP、指定浏览器、指定来源

            /// <summary>
            /// Get读取数据
            /// </summary>
            /// <param name="myUrl">网址</param>
            /// <param name="myProxy">IP代理</param>
            /// <param name="myUserAgent">浏览器</param>
            /// <param name="myReferer">来源</param>
            /// <returns></returns>
            private string HttpGetData(string myUrl, WebProxy myProxy = null, string myUserAgent = "", string myReferer = "")
            {
                try
                {
                    //创建请求
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUrl);
                    if (myProxy != null) request.Proxy = myProxy; //设置代理
                    if (!string.IsNullOrWhiteSpace(myReferer)) request.Referer = myReferer;
                    if (!string.IsNullOrWhiteSpace(myReferer)) request.UserAgent = myUserAgent;//浏览器
    
                    request.Method = "GET";//请求方式
                    request.ContentType = "text/html;charset=UTF-8";//类型
                    request.Timeout = 3000;//超时
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream stream = response.GetResponseStream();
                    string responseStr = "";
                    if (stream != null)
                    {
                        StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                        responseStr = UnicodeToGB(reader.ReadToEnd());
                        reader.Close();
                    }
                    response.Close();
    
                    return responseStr;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
    
    
            /// <summary>
            /// Post提交数据
            /// </summary>
            /// <param name="myUrl">网址</param>
            /// <param name="myParam">参数</param>
            /// <param name="myProxy">IP代理</param>
            /// <param name="myUserAgent">浏览器</param>
            /// <param name="myReferer">来源</param>
            /// <returns></returns>
            public string HttpPostData(string myUrl, string myParam, WebProxy myProxy = null, string myUserAgent = "", string myReferer = "")
            {
                try
                {
                    //创建请求
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUrl);
                    if (myProxy != null) request.Proxy = myProxy; //设置代理
                    if (!string.IsNullOrWhiteSpace(myReferer)) request.Referer = myReferer;
                    if (!string.IsNullOrWhiteSpace(myReferer)) request.UserAgent = myUserAgent;//浏览器
    
                    request.Method = "POST";//请求方式
                    request.ContentType = "application/json;charset=UTF-8";//类型     
                    request.Timeout = 3000;//超时
    
                    byte[] payload = Encoding.UTF8.GetBytes(myParam);//参数编码
                    request.ContentLength = payload.Length;
    
                    //发送post的请求,写入参数
                    Stream writer = request.GetRequestStream();
                    writer.Write(payload, 0, payload.Length);
                    writer.Close();
    
                    //接受返回来的数据
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream stream = response.GetResponseStream();
    
                    string responseStr = "";
                    if (stream != null)
                    {
                        StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                        responseStr = UnicodeToGB(reader.ReadToEnd());
                        reader.Close();
                    }
                    response.Close();
    
                    return responseStr;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
    

      

  • 相关阅读:
    redis你要知道的事
    Nginx七层负载均衡的几种调度算法
    JQuery的父、子、兄弟节点选择器
    laravel通用和万能路由
    jquery竖向企业组织结构图
    phpexcel功能操作
    animatext.js使用介绍
    Centos firewalld开放端口
    获取微信accesstoken并文件缓存方式
    css处理文字不换行、换行截断、溢出省略号
  • 原文地址:https://www.cnblogs.com/wxbug/p/14852113.html
Copyright © 2011-2022 走看看