• 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;
            }
  • 相关阅读:
    程序集“xxx"中的类型"xxx"的方法“XXXX”没有实现
    关于C# json转object时报错:XXXXXXXXXX需要标记“"”,但找到“XX”。
    System.Reflection.Assembly.GetEntryAssembly()获取的为当前已加载的程序集
    MongoDB 模糊查询,及性能测试
    SignalR介绍与Asp.net,前台即时通信【转】
    mongodb常用命令【转】
    MongoDB update数据语法【转】
    Mongodb在Windows下安装及配置 【转】
    C#(asp.net)备份还原mssql数据库代码【转】
    小记,取GB2312汉字的首字母【转】
  • 原文地址:https://www.cnblogs.com/zyx321/p/8527196.html
Copyright © 2020-2023  润新知