HttpWebRequest 多部分表单数据
我正在尝试将简单的文本数据提交到多部分形式的 CGI。
问题是..我不知道如何格式化表单数据!
简而言之,我的系统准备一个包含 post 数据的字符串,使用 request.GetRequestStream() 创建一个流编写器,然后直接将 post 字符串写入该流编写器。然后它继续关闭流编写器并发送请求。
我尝试将字符串格式化为:“Param1=sometext¶m2=sometext”,但返回错误:
Unhandled Exception: System.Net.WebException: The remote server returned an error: (417) Expectation Failed.
at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x002d9] in /private/tmp/monobuild/build/BUILD/mono-2.8/mcs/class/System/System.Net/HttpWebRequest.cs:1425
at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00143] in /private/tmp/monobuild/build/BUILD/mono-2.8/mcs/class/System/System.Net/HttpWebRequest.cs:1250
显然我格式化字符串错误,但我一生都无法弄清楚如何正确执行此操作。
有人可以启发我吗? :)
编辑,我还尝试将 request.ContentType = "application/x-www-form-urlencoded";
更改为 request.ContentType = "multipart/form-data";
。这没有任何作用,但我打算就这样。
编辑:完整代码:
public static string sendReq (string url, string pdata)//Pdata is the string containing form data
{
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
CookieCollection jar = new CookieCollection();
jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
l("Loading cookie jar onto request");
CookieContainer cont = new CookieContainer();
cont.Add(jar);
request.CookieContainer = cont;
if (pdata != "")
{
request.Method = "POST";
request.ContentType = "multipart/form-data;";
request.ContentLength = pdata.Length;
StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(pdata);
stOut.Close();
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
l("Data was read. ["+count+" bytes]. Encoding to ASCII");
tempString = Encoding.ASCII.GetString(buf, 0, count);
l("Appending to full string");
sb.Append(tempString);
}
}
while (count > 0);
return sb.ToString();
}
处理发送帖子数据的代码部分:
if (pdata != "")
{
request.Method = "POST";
request.ContentType = "multipart/form-data;";
request.ContentLength = pdata.Length;
StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(pdata);
stOut.Close();
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
I am trying to submit simple text data to a multipart-form CGI.
The problem is.. I don't know how to format the form data!
In short, my system prepares a string containing the post data, creates a streamwriter using request.GetRequestStream(), and directly writes the post string to that streamwriter. It then goes on to close the streamwriter, and send the request.
I have tried formatting the string as so:"Param1=sometext¶m2=sometext", but I get returned the error:
Unhandled Exception: System.Net.WebException: The remote server returned an error: (417) Expectation Failed.
at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x002d9] in /private/tmp/monobuild/build/BUILD/mono-2.8/mcs/class/System/System.Net/HttpWebRequest.cs:1425
at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00143] in /private/tmp/monobuild/build/BUILD/mono-2.8/mcs/class/System/System.Net/HttpWebRequest.cs:1250
Clearly I am formatting the string wrong, but I cannot for the life of me figure out how to do so properly.
Can someone enlighten me? :)
EDIT, I also tried changing request.ContentType = "application/x-www-form-urlencoded";
to request.ContentType = "multipart/form-data";
. This had no avail, but I am going to leave it that way.
EDIT: Full code:
public static string sendReq (string url, string pdata)//Pdata is the string containing form data
{
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
CookieCollection jar = new CookieCollection();
jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
l("Loading cookie jar onto request");
CookieContainer cont = new CookieContainer();
cont.Add(jar);
request.CookieContainer = cont;
if (pdata != "")
{
request.Method = "POST";
request.ContentType = "multipart/form-data;";
request.ContentLength = pdata.Length;
StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(pdata);
stOut.Close();
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
l("Data was read. ["+count+" bytes]. Encoding to ASCII");
tempString = Encoding.ASCII.GetString(buf, 0, count);
l("Appending to full string");
sb.Append(tempString);
}
}
while (count > 0);
return sb.ToString();
}
The part of code that handles sending the post data:
if (pdata != "")
{
request.Method = "POST";
request.ContentType = "multipart/form-data;";
request.ContentLength = pdata.Length;
StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(pdata);
stOut.Close();
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
显然,HttpWebRequest默认添加了Expect标头,这让很多服务器感到困惑。
更新:
恢复为:
因为内容类型和正文中的数据的当前组合不匹配。
Try:
Obviously, the HttpWebRequest adds an Expect header by default, which confuses a lot of servers.
Update:
An revert to:
because the current combination of the content type and the data in the body do not match.