问题描述
有没有人成功使用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
这是一个完整的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
这篇关于通过HttpWebRequest或WebClient提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!