HttpWebRequest 多部分表单数据

发布于 2024-10-10 08:24:54 字数 3130 浏览 0 评论 0原文

我正在尝试将简单的文本数据提交到多部分形式的 CGI。

问题是..我不知道如何格式化表单数据!

简而言之,我的系统准备一个包含 post 数据的字符串,使用 request.GetRequestStream() 创建一个流编写器,然后直接将 post 字符串写入该流编写器。然后它继续关闭流编写器并发送请求。

我尝试将字符串格式化为:“Param1=sometext&param2=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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2024-10-17 08:24:54

尝试:

System.Net.ServicePointManager.Expect100Continue = false;

显然,HttpWebRequest默认添加了Expect标头,这让很多服务器感到困惑。

更新

恢复为:

request.ContentType = "application/x-www-form-urlencoded";

因为内容类型和正文中的数据的当前组合不匹配。

Try:

System.Net.ServicePointManager.Expect100Continue = false;

Obviously, the HttpWebRequest adds an Expect header by default, which confuses a lot of servers.

Update:

An revert to:

request.ContentType = "application/x-www-form-urlencoded";

because the current combination of the content type and the data in the body do not match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文