第一范文网 - 专业文章范例文档资料分享平台

form表单提交过程

来源:用户分享 时间:2025/6/4 3:43:27 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

浏览器也是一个普通的应用程序,.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表单提交过程 的文档
form表单提交过程.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c032ay6jsj7553972zwpf_4.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top