zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    eclipse使用技巧_慕课文章
    如何将MyEclipse开发的项目导入到Eclipse中运行
    毕设笔记记录
    基于百度地图API的运动轨迹追踪软件思想_ldzbook_新浪博客
    Android实现QQ第三方登录
    CentOS 7 单用户模式修改root密码
    linux下su和su
    python升级3.6后 yum出错File "/usr/bin/yum", line 30 ^
    linux基础命令
    网络基础(osi、协议)
  • 原文地址:https://www.cnblogs.com/sanday/p/11367257.html
Copyright © 2011-2022 走看看