本例使用WebClient以POST方式发送Web请求并下载一个文件,难点是postData的构造,发送Web请求时有的网站要求可能要求 Cookies前后一致。其中application/x-www-form-urlencoded会告诉服务器该参数是以param1=value1& amp;param2=value2&param3=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请求的更多相关文章

  1. python通过get方式,post方式发送http请求和接收http响应-urllib urllib2

    python通过get方式,post方式发送http请求和接收http响应-- import urllib模块,urllib2模块, httplib模块 http://blog.163.com/xyc ...

  2. 在android用Get方式发送http请求

    烦人的日子终于过去啦,终于又可以写博客啦,对自己的android学习做个总结,方便以后查看...... 一.在android用Get方式发送http请求,使用的是java标准类,也比较简单. 主要分以 ...

  3. AJAX方式发送远程请求报错:No 'Access-Control-Allow-Origin' header

    AJAX GET方式发送远程请求,chrome开发者工具console中报错:XMLHttpRequest cannot load http://www.shikezhi.com/ajax/getDa ...

  4. python通过get,post方式发送http请求和接收http响应的方法,pythonget

    python通过get,post方式发送http请求和接收http响应的方法,pythonget 本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法.分享给大家 ...

  5. 安卓基础之Get方式发送http请求

    本文参考作者:超超boy 链接:https://www.cnblogs.com/jycboy/p/post01.html 一.在android用Get方式发送http请求,使用的是java标准类. 主 ...

  6. android中Post方式发送HTTP请求

    Post方式比Get方式要复杂一点,因为该方式需要将请求的参数放在http请求的正文中,所以需要构造请求体. 步骤: 1.构造URL URL url = new URL(PATH); 2.设置连接 H ...

  7. C# 使用WebClient时,在多网卡时,指定IP发送Web请求

    需要定义一个类,重写GetWebRequest,在方法内,指定IP地址 public class MyWebClient : WebClient { private IPAddress ipAddre ...

  8. c# 发送web请求

    我们目前涉及到的现有的接收请求方式有三种, 第一种: 页面式的Form表单 第二种: 服务的webservice形式的xml 第三个: restful风格的post包体json 第一种比较老,博客园的 ...

  9. 安卓中使用OkHttp发送数据请求的两种方式(同、异步的GET、POST) 示例-- Android基础

    1.首先看一下最终效果的截图,看看是不是你想要的,这个年代大家都很忙,开门见山很重要! 简要说下,点击不同按钮可以实现通过不同的方式发送OkHttp请求,并返回数据,这里请求的是网页,所以返回的都是些 ...

随机推荐

  1. linux下Memcached安装以及PHP的调用

    一:安装libevent 由于memcached安装时,需要使用libevent类库,所以先安装libevent 1.官网下载:http://libevent.org/ #wget   https:/ ...

  2. Project Management - 3) Manage Your Meetings

    1. 取消没有价值的会议 会议是有代价和成本的 不要举行顺序式的多人进度报告会议 eg: 这周做了什么,下周还要做什么? 除了发言人和项目经理外,每个人都会觉得无聊. 这种会议是在拖项目的后腿,赶紧停 ...

  3. js之createTextRange方法

    createTextRange()方法作用: 主要是用来对一些文本对象进行操作.比如你有一大段文字,都在同一个P标签内,但是你只希望通过JS改变其中的一小部分,这时就可以用createTextRang ...

  4. java_JdbcUtilis_单实例

    //eg1,没有使用单实例,eg2有 package cn.itcast; import java.sql.Connection; import java.sql.DriverManager; imp ...

  5. java使用Thumbnailator操作图片

    Thumbnailator 是一个用来生成图像缩略图.裁切.旋转.添加水印等操作的 Java 类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图. Thumbnailato ...

  6. How to Tune Java Garbage Collection--reference

    reference:http://architects.dzone.com/articles/how-tune-java-garbage The Performance Zone is support ...

  7. nodeValue和innerHTML的比较

    <ul id="parent"> <li>1</li> <li>2</li> <li>3</li> ...

  8. Java阻塞中断和LockSupport

    在介绍之前,先抛几个问题. Thread.interrupt()方法和InterruptedException异常的关系?是由interrupt触发产生了InterruptedException异常? ...

  9. strstr,strchr,strtr 比较

    strstr - 查找字符串的首次出现 查找 "Shanghai" 在字符串中的第一次出现,并返回字符串的剩余部分: echo strstr("I love Shangh ...

  10. 蓝牙 GameKit

    一.准备工作 1.搭建UI  2.拖线 // 图片 @property (weak, nonatomic) IBOutlet UIImageView *imageView; // 建立连接 - (IB ...