• C# HttpRequest 请求


    public static string Post(string Url, string postDataStr, string cookies)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                if (cookies != null)
                    request.Headers.Add("Cookie", cookies);
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postDataStr.Length;
                StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
                writer.Write(postDataStr);
                writer.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码  
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string retString = reader.ReadToEnd();
                return retString;
            }
    
            public static string Get(string Url, string cookies)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "GET";
                if (cookies != null)
                    request.Headers.Add("Cookie", cookies);
                request.ContentType = "application/x-www-form-urlencoded";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码  
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string retString = reader.ReadToEnd();
                return retString;
            }
  • 相关阅读:
    如何显示在线人数和所在位置
    Global.asax的使用点滴 (一)
    怎样使编译后,Session值不丢失。
    VB.NET 开发AutoCAD中常见错误
    GDI 虚线画笔
    VB.NET阻止程序重复启动
    GDI文字对齐
    js获取客户端浏览器信息
    DOM 基础知识
    JavaScript 不支持重载
  • 原文地址:https://www.cnblogs.com/sanday/p/11367257.html
Copyright © 2020-2023  润新知