zoukankan      html  css  js  c++  java
  • Http请求

    public static string RequestPost(string Url, string parameter, string ContentType = "application/x-www-form-urlencoded")
            {
    
                HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(Url);
                hwrq.Method = "Post";
                hwrq.ContentType = ContentType;//application/x-www-form-urlencoded
                if (parameter != "")
                {
                    byte[] bt = Encoding.UTF8.GetBytes(parameter);
                    ////byte[] bt = Encoding.GetEncoding("gbk").GetBytes(d);
                    hwrq.ContentLength = bt.Length;
                    Stream sw = hwrq.GetRequestStream();
                    sw.Write(bt, 0, bt.Length);
                    sw.Close();
                }
                HttpWebResponse res = null;
                HttpWebResponse hwrp1 = null;
                try
                {
                    hwrp1 = (HttpWebResponse)hwrq.GetResponse();
                    string strlcHtml = string.Empty;
                    Encoding enc = Encoding.GetEncoding("UTF-8");
                    Stream stream = hwrp1.GetResponseStream();
                    StreamReader streamReader = new StreamReader(stream, enc);
                    strlcHtml = streamReader.ReadToEnd();
                    return strlcHtml;
                }
                catch (WebException ex)
                {
                    res = (HttpWebResponse)ex.Response;
                    StreamReader sr = new StreamReader(res.GetResponseStream(), true);
                    string strHtml = sr.ReadToEnd();
                    return strHtml;
                }
            }
            public static string RequestGet(string Url)
            {
                try
                {
                    string strUrl = Url;
                    HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(strUrl);
                    hwrq.Method = "GET";
                    HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse();
                    HttpWebResponse hwrp1 = null;
    
                    hwrp1 = (HttpWebResponse)hwrq.GetResponse();
                    Stream stream = hwrp1.GetResponseStream();
                    Encoding enc = Encoding.GetEncoding("UTF-8");
                    StreamReader streamReader = new StreamReader(stream, enc);
                    string strlcHtml = streamReader.ReadToEnd();
                    return strlcHtml;
                }
                catch (Exception ex)
                {
                    new LogManager().WriteLine("RequestGet获取数据错误:" + ex.Message + ";请求地址:" + Url);
                    return "";
                }
    
            }
  • 相关阅读:
    Ubuntu 分辨率显示出错,分辨率不是最佳分辨率的解决办法
    xmr monero miner
    Win+数字快速启动/切换指定程序
    ajax 提交 form表单 ,后台执行两次的问题
    uploadify HTTP 302 错误如何解决?
    Uploadifive 上传'fileType'格式如何限制?
    服务器更新了php版本报错(PHP7.3)
    SourceTree软件
    Csdn账号如何注销?
    鸡翅怎么做好吃呢?
  • 原文地址:https://www.cnblogs.com/BoyStyle/p/8978857.html
Copyright © 2011-2022 走看看