通过HttpWebRequest或WebClient提交表单 [英] submit form via HttpWebRequest or WebClient

查看:58
本文介绍了通过HttpWebRequest或WebClient提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人成功使用HttpWebRequest或WebClient类来模拟提交一个简单的HTML表单?


例如:一个非常简单的普通香草形式使用文本框和按钮。

单击按钮时,表单将与文本框内容一起提交。


你能发一些示例代码吗?谢谢。

has anyone successfully used HttpWebRequest or WebClient class to simulate
submission of a simple HTML form?

for example: a very simple plain-vanilla form with a textbox and a button.
when the button is clicked the form is submitted with the textbox contents.

could you please post some sample code? thanks.

推荐答案

John A Grandy写道:
John A Grandy wrote:
有人成功使用HttpWebRequest或WebClient类进行模拟提交一个简单的HTML表单?

例如:一个非常简单的普通香草形式,带有文本框和
按钮。当单击按钮时,表单将与
文本框内容一起提交。

您能否发布一些示例代码?谢谢。
has anyone successfully used HttpWebRequest or WebClient class to
simulate submission of a simple HTML form?

for example: a very simple plain-vanilla form with a textbox and a
button. when the button is clicked the form is submitted with the
textbox contents.

could you please post some sample code? thanks.




这是一个完整的WebRequest / WebResponse示例,包括URL编码。

public void PostForm(string url,string formData, string encodingName){

// URL对帖子数据进行编码,应用

// encodingName给出的字符编码。

MemoryStream content = new MemoryStream(formData.Length * 2);

编码编码= Encoding.GetEncoding(encodingName);

string [] keyValuePairs = formData.Split(''&''' ,''='');

int numberOfPairs = keyValuePairs.Length;

bool isKey = true;

foreach(keyValuePairs中的字符串keyValue ){

byte [] bytes = HttpUtility.UrlEncodeToBytes(keyValue,encoding);

content.Write(bytes,0,bytes.Length);

if(isKey){

content.WriteByte((byte)''='');

}

else {

content.WriteByte((byte)''&'');

}

isKey =!is钥匙;

}

//我们最终会有一个剩余''&'' - 不应该受伤,但我们会把它剪掉/>
无论如何

content.SetLength(content.Length - 1);


//这是实际的HTTP部分样本

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

request.Method =" POST";

request.ContentType = String.Format(

" application / x-www-form-urlencoded; charset = {0}",encodingName);

request.ContentLength = content.Length;

using(Stream requestStream = request.GetRequestStream()){

content.WriteTo(requestStream);

}


HttpWebResponse response =(HttpWebResponse)request.GetResponse();

using(Stream responseStream = response.GetResponseStream()){

byte [] buffer = new byte [response.ContentLength];

responseStream.Read(buffer, 0,buffer.Length);

//处理缓冲区...例如写入文件,解码为字符串等。

}

}


干杯,


-

Joerg Jooss
jo ****** ***@gmx.net


嗨约翰:


我在这里有一个例子:


使用ASP.Net进行屏幕刮擦,ViewState和身份验证
http://odetocode.com/Articles/162.aspx


这也显示了如何从asp.net页面中提取视图状态到<回馈。
回复。


HTH,


-

Scott
http://www.OdeToCode.com


On Mon,2004年8月23日23:23:31 -0700,John A Grandy

< johnagrandy-at-yahoo.com>写道:
Hi John:

I have an example here:

Screen Scraping, ViewState, and Authentication using ASP.Net
http://odetocode.com/Articles/162.aspx

This also shows how to extract the viewstate out of an asp.net page to
post back.

HTH,

--
Scott
http://www.OdeToCode.com

On Mon, 23 Aug 2004 23:23:31 -0700, "John A Grandy"
<johnagrandy-at-yahoo.com> wrote:
有没有人成功使用HttpWebRequest或WebClient类来模拟提交一个简单的HTML表单?

例如:一个非常简单的平原 - 带有文本框和按钮的形式。
单击按钮时,表单将与文本框内容一起提交。

请您发布一些示例代码?谢谢。
has anyone successfully used HttpWebRequest or WebClient class to simulate
submission of a simple HTML form?

for example: a very simple plain-vanilla form with a textbox and a button.
when the button is clicked the form is submitted with the textbox contents.

could you please post some sample code? thanks.






所以无论用户输入文本框textbox1的数据是什么,我都会支付
通过撰写字符串来模拟


formData =" textbox1 = thedata"


但我怎么知道encodingName应该是什么?

" Joerg Jooss" <乔********* @ gmx.net>在消息中写道

news:ua ************** @ tk2msftngp13.phx.gbl ...
so whatever data the user would enter into the text box "textbox1", i
simulate by composing the string

formData="textbox1=thedata"

but how do i know what encodingName should be ?
"Joerg Jooss" <jo*********@gmx.net> wrote in message
news:ua**************@tk2msftngp13.phx.gbl...
John A Grandy写道:
John A Grandy wrote:
有没有人成功使用HttpWebRequest或WebClient类来模拟提交一个简单的HTML表单?

例如:一个非常简单的带有文本框的普通表单和一个
按钮。当单击按钮时,表单将与
文本框内容一起提交。

您能否发布一些示例代码?谢谢。
has anyone successfully used HttpWebRequest or WebClient class to
simulate submission of a simple HTML form?

for example: a very simple plain-vanilla form with a textbox and a
button. when the button is clicked the form is submitted with the
textbox contents.

could you please post some sample code? thanks.



这是一个完整的WebRequest / WebResponse示例,包括URL编码。

public void PostForm(string url,string formData,string encodingName){
// URL对帖子数据进行编码,应用
// encodingName给出的字符编码。
MemoryStream content = new MemoryStream(formData.Length * 2);
编码编码= Encoding.GetEncoding(encodingName);
string [] keyValuePairs = formData.Split(''&'',''='');
int numberOfPairs = keyValuePairs.Length;
bool isKey = true;
foreach(keyValuePairs中的字符串keyValue){
byte [] bytes = HttpUtility.UrlEncodeToBytes(keyValue,encoding);
content.Write(bytes,0,bytes.Length) ;
if(isKey){
content.WriteByte((byte)''='');
}
else {
content.WriteByte((byte) ''&'');
}
isKey =!isKey;
}
//我们最终会得到一个剩余的''&'' - 不应该受伤,但我们还是把它剪掉了

content.SetLength(content.Length - 1);

//这是样本的实际HTTP部分
HttpWebRequest request =(HttpWebRequest)WebRequest.Create( url);
request.Method =" POST";
request.ContentType = String.Format(
" application / x-www-form-urlencoded; charset = {0}",encodingName);
request.ContentLength = content.Length;
using(Stream requestStream = request.GetRequestStream()){
content.WriteTo(requestStream);
}

HttpWebResponse response =(HttpWebResponse)request.GetResponse();
using(Stream responseStream = response.GetResponseStream()){
byte [] buffer = new byte [response.ContentLength];
responseStream.Read(buffer,0,buffer.Length);
//进程缓冲区...例如写入文件,解码为字符串等。
}

干杯,

-
Joerg Jooss
jo*********@gmx.net



这篇关于通过HttpWebRequest或WebClient提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆