C# post请求 HttpWebRequest

//body是要传递的参数,格式"roleId=1&uid=2"
//post的cotentType填写:
//"application/x-www-form-urlencoded"
//soap填写:"text/xml; charset=utf-8"

public  string PostHttp(string url, string body, string contentType)

{

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

httpWebRequest.ContentType = contentType;

httpWebRequest.Method = "POST";

httpWebRequest.Timeout = 20000;

byte[] btBodys = Encoding.UTF8.GetBytes(body);

httpWebRequest.ContentLength = btBodys.Length;

httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);

HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());

string responseContent = streamReader.ReadToEnd();

httpWebResponse.Close();

streamReader.Close();

httpWebRequest.Abort();

httpWebResponse.Close();

return responseContent;

}

时间: 2024-08-10 03:53:41

C# post请求 HttpWebRequest的相关文章

网络请求HttpWebRequest的Get和Post

用下边的小例子讲解具体功能的实现: 首先,我们想要请求远程站点,需要用到HttpWebRequest类,该类在System.Net命名空间中,所以需要引用一下.另外,在向请求的页面写入参数时需要用到Stream流操作,所以需要引用System.IO命名空间. 以下为Get请求方式: Uri uri = new Uri("http://www.cnsaiko.com/");//创建uri对象,指定要请求到的地址 if (uri.Scheme.Equals(Uri.UriSchemeHtt

后端向服务器发送客户端请求--HttpWebRequest

HttpWebRequest类与HttpRequest类的区别 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所有信息. HttpWebRequest用于客户端,拼接请求的HTTP报文并发送等.它封装了几乎HTTP请求报文里需要用到的东西,以致于能够能够发送任意的HTTP请求并获得服务器响应(Response)信息.采集信息常用到这个类 1 private ApiResultModel GetDataByProductIdInApi(int P

c# HttpWebRequest与HttpWebResponse请求网页和返回网页教程

如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地址获取网页信息 先来看一下代码 get方法 C# Code复制内容到剪贴板 public static string GetUrltoHtml(string Url,string type) { try { System.Net.WebRequest wReq = System.Net.WebReq

C#利用 HttpWebRequest 类发送post请求,出现“套接字(协议/网络地址/端口)只允许使用一次”问题

声明:问题虽然已经被解决,但是并没有明白具体原理,欢迎大佬补充. 最近网站出现一个问题,在C#里面使用  HttpWebRequest 类去发送post请求,偶尔 会出现 “套接字(协议/网络地址/端口)只允许使用一次” 的异常,很明显应该是端口被占用. 原因排查: 1.网上说最多就是其他程序占用端口:因为已经上线,并且有时候可以正常运行,因此排除其他程序占用端口的可能,而且我网站用的就是80端口. 2.第二个可能原因就是接口性能较差,占用较长处理时间:我 给post的目标接口(因为接口也是本身

发送Http Get和Post请求

发送Get请求 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Timeout = config.Timeout;//设置超时时间 HttpWebResponse res = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(res.GetResponseStream(),

C#中http请求下载的常用方式demo

//通过webClient方式 WebClient client = new WebClient(); string url="http://down6.3987.com:801/2010/office_3987.com.zip"; Stream strm = client.OpenRead("http://down6.3987.com:801/2010/office_3987.com.zip"); string filename=url.Substring(url

.net 发送get/post请求

一.使用 string url = "http://www.example.com/api/exampleHandler.ashx"; var parameters = new Dictionary<string, string>(); parameters.Add("param1", "1"); parameters.Add("param2", "2"); string result = se

C#模拟登录后请求查询

需求是这样子的,想开发一个外挂程序,能够抓取别的系统的数据,从而实现数据验证. 比如这样一个界面: 使用Chrome浏览器分析http请求和响应过程以及页面的html代码,发现这是一个ajax请求,于是跟踪找到了具体的请求地址和查询时提交的数据. 于是就可以请求这个地址,并且封装提交的数据进行http请求即可. 但实验后发现,需要先登录系统然后才能进行查询请求. 分析系统登录部分代码发现,仍然是一个ajax post请求后台的代码,截图如下: 从js代码可以看出res=899为登录失败,其它为登

C# HttpWebRequest 绝技 根据URL地址获取网页信息

如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地址获取网页信息 先来看一下代码 get方法 复制代码 publicstaticstring GetUrltoHtml(string Url,string type) { try { System.Net.WebRequest wReq = System.Net.WebRequest.Create(U