开发者

HttpWebRequest multipart form-data

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 s开发者_开发问答tring as so:"Param1=sometext&param2=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();


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜