• HTTP请求


            public string HttpPost(string Url, string postDataStr = "", string access_token = "")
            {
    
                //CookieContainer cookie = new CookieContainer();
    
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                if (!string.IsNullOrWhiteSpace(access_token))
                {
    
                    request.Headers.Add("Authorization", access_token);
                }
                request.Method = "POST";
                //request.ContentType = "application/json;charset=utf-8";
                //request.ContentType = "application/json;charset=gb2312";
    
                if (!string.IsNullOrWhiteSpace(postDataStr))
                {
                    //request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
                }
    
                //request.CookieContainer = cookie;
                Stream myRequestStream = request.GetRequestStream();
    
                //发送时用的编码
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream);//, Encoding.GetEncoding("gb2312"));
                //StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8"));
                if (!string.IsNullOrWhiteSpace(postDataStr))
                {
                    myStreamWriter.Write(postDataStr);
                }
                myStreamWriter.Close();
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                //response.Cookies = cookie.GetCookies(response.ResponseUri);
                Stream myResponseStream = response.GetResponseStream();
    
                //接收时用的编码
                //StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                StreamReader myStreamReader = new StreamReader(myResponseStream);//, Encoding.GetEncoding("gb2312"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
    
                return retString;
            }
    
            public string HttpGet(string Url, string postDataStr = "", string access_token = "")
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                if (!string.IsNullOrWhiteSpace(access_token))
                {
    
                    request.Headers.Add("Authorization", access_token);
                }
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
    
                return retString;
            }
  • 相关阅读:
    IIS7中启用JS的压缩
    .NET中使用WINDOWS API参数定义
    IE6下的JQUERY_FCK兼容问题
    移动native+HTML5混搭应用感受
    【翻译】Fixie.js——自动填充内容的插件
    JSON辅助格式化
    【记录】导致notifyDataSetChanged无效的一个错误
    【Perl】批量word和PPT文档转pdf
    【记录】有关parseInt的讨论
    手机滑动应用
  • 原文地址:https://www.cnblogs.com/zyx321/p/8527196.html
Copyright © 2020-2023  润新知