WebClient以POST方式发送Web请求
本例使用WebClient以POST方式发送Web请求并下载一个文件,难点是postData的构造,发送Web请求时有的网站要求可能要求 Cookies前后一致。其中application/x-www-form-urlencoded会告诉服务器该参数是以param1=value1& amp;param2=value2¶m3=value3方式拼接的。
private bool postDataandDownloadFile(string fileName)
{
string url = "http://www.huiyaosoft.com/test.aspx";
StringBuilder postData = new StringBuilder();
postData.AppendFormat("{0}={1}&", "username", "admin");
postData.AppendFormat("{0}={1}&", "password", "");
postData.AppendFormat("{0}={1}&", "nickname", UrlEncode("辉耀"));
try
{
if (wc == null)
wc = new System.Net.WebClient();
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); // 继承Cookies
if (!string.IsNullOrEmpty(cookies))
wc.Headers.Add("Cookie", cookies); // Upload the input string using the HTTP 1.0 POST method.
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(postData.ToString());
// 此处返回的是一个文件
byte[] byteResult = wc.UploadData(url, "POST", byteArray);
// 取得Cookies
cookies = wc.ResponseHeaders["Set-Cookie"]; if (type == || type == )
writeFile(byteResult, fileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return false;
}
由于设置了"Content-Type"为"application/x-www-form-urlencoded",所以postData必须先进行urlencode。UrlEncode()的作用是将参数进行编码。
public string UrlEncode(string str)
{
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
return System.Web.HttpUtility.UrlEncode(byStr);
}
将返回的数据写入文件
//写byte[]到fileName
private bool writeFile(byte[] pReadByte, string fileName)
{
FileStream pFileStream = null;
try
{
pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
pFileStream.Write(pReadByte, , pReadByte.Length);
}
catch
{
return false;
}
finally
{
if (pFileStream != null)
pFileStream.Close();
}
return true;
}
(万)
WebClient以POST方式发送Web请求的更多相关文章
- python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
python通过get方式,post方式发送http请求和接收http响应-- import urllib模块,urllib2模块, httplib模块 http://blog.163.com/xyc ...
- 在android用Get方式发送http请求
烦人的日子终于过去啦,终于又可以写博客啦,对自己的android学习做个总结,方便以后查看...... 一.在android用Get方式发送http请求,使用的是java标准类,也比较简单. 主要分以 ...
- AJAX方式发送远程请求报错:No 'Access-Control-Allow-Origin' header
AJAX GET方式发送远程请求,chrome开发者工具console中报错:XMLHttpRequest cannot load http://www.shikezhi.com/ajax/getDa ...
- python通过get,post方式发送http请求和接收http响应的方法,pythonget
python通过get,post方式发送http请求和接收http响应的方法,pythonget 本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法.分享给大家 ...
- 安卓基础之Get方式发送http请求
本文参考作者:超超boy 链接:https://www.cnblogs.com/jycboy/p/post01.html 一.在android用Get方式发送http请求,使用的是java标准类. 主 ...
- android中Post方式发送HTTP请求
Post方式比Get方式要复杂一点,因为该方式需要将请求的参数放在http请求的正文中,所以需要构造请求体. 步骤: 1.构造URL URL url = new URL(PATH); 2.设置连接 H ...
- C# 使用WebClient时,在多网卡时,指定IP发送Web请求
需要定义一个类,重写GetWebRequest,在方法内,指定IP地址 public class MyWebClient : WebClient { private IPAddress ipAddre ...
- c# 发送web请求
我们目前涉及到的现有的接收请求方式有三种, 第一种: 页面式的Form表单 第二种: 服务的webservice形式的xml 第三个: restful风格的post包体json 第一种比较老,博客园的 ...
- 安卓中使用OkHttp发送数据请求的两种方式(同、异步的GET、POST) 示例-- Android基础
1.首先看一下最终效果的截图,看看是不是你想要的,这个年代大家都很忙,开门见山很重要! 简要说下,点击不同按钮可以实现通过不同的方式发送OkHttp请求,并返回数据,这里请求的是网页,所以返回的都是些 ...
随机推荐
- 看文章《EAI和SOA的比较》有感(1)
<EAI和SOA的比较>http://www.cnblogs.com/asdling/archive/2007/11/26/973100.html这篇文章写的很全面,至少自己这么认为,也解 ...
- 【54】让自己熟悉包括TR1在内的标准程序库
1.C++0X,不确定哪一年出来,意指200X版的C++ 2.C++标准程序库的主要机能有:STL,iostreams,locals等. 3.TR1:Technical Report 1,只是一份规范 ...
- UVa 131 - The Psychic Poker Player
题目:手里有五张牌,桌上有一堆牌(五张).你能够弃掉手中的k张牌,然后从牌堆中取最上面的k个. 比較规则例如以下:(按优先级排序) 1.straight-flush:同花顺.牌面为T(10) - A, ...
- rk3288的SDK修复cm3218光敏驱动bug
瑞芯的Android 4.4的SDK中kernel的补丁例如以下: diff --git a/arch/arm/boot/dts/rk3288-tb_8846.dts b/arch/arm/boot/ ...
- 百度之星资格赛,hdu 4825 XOR SUM
显然是要建一棵0.1树 事实上非常easy就是二叉树,仅仅只是为了操作简便,即程序的速度,所以就採用静态树,即不动态分配内存,使用较大的全局数组.0是根节点 #include<cstdio> ...
- linux-insides-cn-BOOK
https://www.gitbook.com/book/xinqiu/linux-insides-cn/details https://www.gitbook.com/book/looly/elas ...
- Android(java)学习笔记154:使用GridView以及重写BaseAdapter
1.BaseAdapter: 对于ListView.GridView.Gallery.Spinner等等,它是它们的适配器,直接继承自接口类Adapter的,使用BaseAdapter时需要重写很多方 ...
- sscanf、strsep
#include <stdio.h> #include <string.h> int main() { char token[] ="abdzxbcdefgh&quo ...
- SharePoint缓存导致访问慢解决
产品发布到公网时,客户每次访问页面都很慢,经过查找原因,发现在服务器上的APPFabric缓存出错误了: APPFabric缓存服务作用:用作内存中缓存来存储应用程序访问的数据,从而提高应用程序性能. ...
- 页面javascript 和jquery 的一些用法
confirm:提交之前的提示验证 if(confirm("确认是否提交?")){ alert("确认提交..."); } input按钮的禁用: $(&quo ...