云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > form表单提交过程

form表单提交过程

  • 62 次阅读
  • 3 次下载
  • 2026/1/24 13:41:39

浏览器也是一个普通的应用程序,.net framework也提供一些类也能让我们直接发起HTTP请求。今天我将再次用C#来模拟浏览器的提交请求,同时也可以加深对HTTP请求的理解。 示例代码分为二段,一段示范了使用application/x-www-form-urlencoded编码方式提交,另一段则示范了使用multipart/form-data的编码方式。 为了让大家能再次利用这些代码,我已将关键部分写成独立方法,希望当您有这方面的需求时能马上可以用上。代码如下: 1. application/x-www-form-urlencoded

///

/// 向指定的URL地址发起一个POST请求,同时可以上传一些数据项。 ///

///

///

/// /// 服务器的返回结果

static string SendHttpRequestPost(string url, Dictionary keyvalues, Encoding encoding) {

if( string.IsNullOrEmpty(url) )

throw new ArgumentNullException(\);

string postData = null;

// 将数据项转变成 name1=value1&name2=value2 的形式 if( keyvalues != null && keyvalues.Count > 0 ) { postData = string.Join(\, (from kvp in keyvalues

let item = kvp.Key + \+ HttpUtility.UrlEncode(kvp.Value) select item ).ToArray() ); }

if( encoding == null )

encoding = Encoding.UTF8;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = \;

request.ContentType = \charset=\+ encoding.WebName;

if( postData != null ) {

byte[] buffer = encoding.GetBytes(postData);

Stream stream = request.GetRequestStream(); stream.Write(buffer, 0, buffer.Length); stream.Close(); }

using( WebResponse response = request.GetResponse() ) { using( StreamReader reader = new

StreamReader(response.GetResponseStream(), encoding) ) { return reader.ReadToEnd(); } } }

// 调用上面方法的示例代码

string Test_SendHttpRequestPost() {

string url = \;

Dictionary keyvalues = new Dictionary();

keyvalues.Add(\, \我是李奇峰,$%@+& ?#^/\); keyvalues.Add(\, \);

return SendHttpRequestPost(url, keyvalues, null); }

2. multipart/form-data 。注意这部分代码有点复杂,因此我加了很多注释。

///

/// 向指定的URL地址发起一个POST请求,同时可以上传一些数据项以及上传文件。

///

///

/// ///

/// /// 服务器的返回结果

static string SendHttpRequestPost(string url, Dictionary keyvalues,

Dictionary fileList, Encoding encoding) {

if( fileList == null )

return SendHttpRequestPost(url, keyvalues, encoding);

if( string.IsNullOrEmpty(url) )

throw new ArgumentNullException(\);

if( encoding == null )

encoding = Encoding.UTF8;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = \; // 要上传文件,一定要是POST方法

// 数据块的分隔标记,用于设置请求头,注意:这个地方最好不要使用汉字。

string boundary = \+ Guid.NewGuid().ToString(\);

// 数据块的分隔标记,用于写入请求体。

// 注意:前面多了一段: \,而且它们将独占一行。

byte[] boundaryBytes = Encoding.ASCII.GetBytes(\+ boundary + \);

// 设置请求头。指示是一个上传表单,以及各数据块的分隔标记。

request.ContentType = \+ boundary;

// 先得到请求流,准备写入数据。

Stream stream = request.GetRequestStream();

if( keyvalues != null && keyvalues.Count > 0 ) { // 写入非文件的keyvalues部分

foreach( KeyValuePair kvp in keyvalues ) { // 写入数据块的分隔标记

stream.Write(boundaryBytes, 0, boundaryBytes.Length);

// 写入数据项描述,这里的Value部分可以不用URL编码 string str = string.Format(

\name=\\\,

kvp.Key, kvp.Value);

byte[] data = encoding.GetBytes(str); stream.Write(data, 0, data.Length); } }

// 写入要上传的文件

foreach( KeyValuePair kvp in fileList ) { // 写入数据块的分隔标记

stream.Write(boundaryBytes, 0, boundaryBytes.Length);

// 写入文件描述,这里设置一个通用的类型描述:application/octet-stream,具体的描述在注册表里有。 string description = string.Format(

\filename=\\\+

\, kvp.Key, Path.GetFileName(kvp.Value));

// 注意:这里如果不使用UTF-8,对于汉字会有乱码。 byte[] header = Encoding.UTF8.GetBytes(description); stream.Write(header, 0, header.Length);

// 写入文件内容

byte[] body = File.ReadAllBytes(kvp.Value); stream.Write(body, 0, body.Length); }

// 写入结束标记

boundaryBytes = Encoding.ASCII.GetBytes(\+ boundary + \);

stream.Write(boundaryBytes, 0, boundaryBytes.Length);

stream.Close();

// 开始发起请求,并获取服务器返回的结果。

using( WebResponse response = request.GetResponse() ) { using( StreamReader reader = new

StreamReader(response.GetResponseStream(), encoding) ) { return reader.ReadToEnd(); } } }

// 调用上面方法的示例代码

string Test_SendHttpRequestPost2() {

string url = \;

Dictionary keyvalues = new Dictionary();

keyvalues.Add(\, \本示例代码由 Fish Li 提供\);

keyvalues.Add(\, \); keyvalues.Add(\, \来几个特殊字符:~!@#$%^&*()-=_+{}[]:;'\\\);

Dictionary fileList = new Dictionary();

fileList.Add(\, @\中文字.gif\); fileList.Add(\, @\中文字.gif\);

return SendHttpRequestPost(url, keyvalues, fileList, Encoding.UTF8); }

说明:上面的示例方法中,我并没有对KEY编码,是因为:我想大家选用的KEY应该是不需要编码的(英文字母与数字的组合)。

搜索更多关于: form表单提交过程 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

浏览器也是一个普通的应用程序,.net framework也提供一些类也能让我们直接发起HTTP请求。今天我将再次用C#来模拟浏览器的提交请求,同时也可以加深对HTTP请求的理解。 示例代码分为二段,一段示范了使用application/x-www-form-urlencoded编码方式提交,另一段则示范了使用multipart/form-data的编码方式。 为了让大家能再次利用这些代码,我已将关键部分写成独立方法,希望当您有这方面的需求时能马上可以用上。代码如下: 1. application/x-www-form-urlencoded /// /// 向指定的URL地址发起一个POST请求,同时可以上传一些数据项。 /// ///

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com