博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Http请求
阅读量:5214 次
发布时间:2019-06-14

本文共 2415 字,大约阅读时间需要 8 分钟。

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 "";            }        }

 

转载于:https://www.cnblogs.com/BoyStyle/p/8978857.html

你可能感兴趣的文章
Python命名规范
查看>>
滚动条
查看>>
程序员的自我修养九Windows下的动态链接
查看>>
Codeforces Round #361 (Div. 2)
查看>>
细说WebSocket - Node篇
查看>>
jenkins+testNG
查看>>
[洛谷1485] 火枪打怪
查看>>
PAT B1018.锤子剪刀布(20)
查看>>
Extjs控件之 grid打印功能
查看>>
枚举类型(不常用)递归
查看>>
ETL
查看>>
Tomcat源码分析(六)--日志记录器和国际化
查看>>
MySQL学习笔记(二):MySQL数据类型汇总及选择参考
查看>>
YII缓存依赖的应用
查看>>
决策树在机器学习的理论学习与实践
查看>>
Biee 11g权限详解
查看>>
minggw 安装
查看>>
Jquery操作cookie,实现简单的记住用户名的操作
查看>>
[BZOJ1196][HNOI2006]公路修建问题 二分答案+最小生成树
查看>>
PHP基础入门(二)
查看>>