• HTTP请求方法


     #region 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";
    
                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"));
                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;
            }
    
            #endregion
  • 相关阅读:
    [ZZ]终极期望
    推荐一部好电影
    读书笔记:《Java2 教程》(五)
    波音飞机的消息
    雪景
    [ZZ]候捷谈Java反射机制
    关于J2ME开发的感想(20060505更新)
    读书笔记:《Java2 教程》(七)
    读书笔记:《Java2 教程》(六)
    注册了Bloglines
  • 原文地址:https://www.cnblogs.com/zyx321/p/8351681.html
Copyright © 2020-2023  润新知