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

C# post请求 HttpWebRequest的更多相关文章

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

    HttpWebRequest类与HttpRequest类的区别 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所有信息. HttpWebReques ...

  2. POST请求——HttpWebRequest

    string url=""; string param=""; string result = string.Empty; HttpWebRequest req ...

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

    声明:问题虽然已经被解决,但是并没有明白具体原理,欢迎大佬补充. 最近网站出现一个问题,在C#里面使用  HttpWebRequest 类去发送post请求,偶尔 会出现 “套接字(协议/网络地址/端 ...

  4. C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用

    public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cooki ...

  5. C# 应用 - 使用 HttpWebRequest 发起 Http 请求

    helper 类封装 调用 1. 引用的库类 \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.dll Syste ...

  6. C# http请求数据

    http中get和post请求的最大区别:get是通过URL传递表单值,post传递的表单值是隐藏到 http报文体中 http以get方式请求数据 /// <summary> /// g ...

  7. c# HttpWebRequest 与 HttpWebResponse

    如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的Get和Post方式进行详细的说明,在请求时的参数怎么发送,怎么带Cookie,怎么设置证 ...

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

    如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地 ...

  9. C# HttpWebRequest 绝技 转至 http://www.sufeinet.com/

    转至: 在线测试工具http://www.sufeinet.com/thread-3690-1-1.htmlc# HttpWebRequest与HttpWebResponse 绝技    如果你想做一 ...

随机推荐

  1. iOS学习之数据请求

    GET请求----同步连接 //GET请求 同步连接 - (void)handleSynchronize:(UIBarButtonItem *)item { //GET请求 //1.创建网址字符串; ...

  2. uC/OS-II中的时间 (转)

    时间是一个非常重要的概念,我们和朋友出去游玩需要约定一个时间,做事情也需要花费一段时间,总之,我们的生活离不开时间.操作系统也一样,也需要一个时间来规范其任务的执行. 我们生活中,时间的最小单位是秒, ...

  3. android手机端保存xml数据

    1.前面写的这个不能继续插入数据,今天补上,当文件不存在的时候就创建,存在就直接往里面添加数据. 2.代码如下: <pre name="code" class="j ...

  4. hdu 1301 Jungle Roads

    http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...

  5. 如何修改Qt标准对话框的文字(例如,英文改成中文)

    此篇文章参考qtcn论坛整理而成,因为文字和图片是本人亲自组织,所以仍算原创. http://www.qtcn.org/bbs/read-htm-tid-30650.html http://blog. ...

  6. (Stack)Basic Calculator I && II

    Basic Calculator I Implement a basic calculator to evaluate a simple expression string. The expressi ...

  7. hdu4641-K-string(后缀自动机)

    Problem Description Given a string S. K-string is the sub-string of S and it appear in the S at leas ...

  8. samsungGalaxyS4USB驱动

    http://www.samsung.com/cn/support/usefulsoftware/KIES/JSP

  9. TableView 校检表

    这俩天学习了tableView 校检表 主要就是通过一个方法来跟踪当前选中的行.下面将声明一个NSIndexPath 的属性来跟踪最后选中的行.这篇文章希望能给那些初学者带来学习的乐趣.不说了直接上代 ...

  10. 今天弄了整整一天DCloud

    开发一个新闻移动客户端 起先用appcan.apiCloud,最后选择了DCloud: MUI文档看了n久,js代码一上来不很适应编码风格,一堆括号弄得头大: 数据库由爬虫程序将新闻页面数据爬出来: ...