views:

73

answers:

1

I am sending an HTTP post request using HTTPWebRequest to an URL. I am sending the post data using multipart/form-data content type along with the content length of the body. However, on the server side, I am unable to retrieve the body. I can only see the headers sent. The content length of the body I sent also matches.

Why am I not able to retrieve the body.

The request method looks like this:

public void Reset(string originalFileData, string uploadLocation)
    {
        TcpClient client = new TcpClient();
        IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(Server), portNo);
        client.Connect(serverEndPoint);
        string responseContent;
        string serverUrl = "http://" + Server + ":" + portNo + "/abc.aspx" + "?uplvar=" + uploadLocation;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUrl);
        request.ContentType = "multipart/form-data";
        request.Method = "POST";
        request.ServicePoint.Expect100Continue = false;
        string postData = originalFileData;
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        response.Close();
    }

Edit: I forgot to metion, I am able to retrieve the body on the first time I send the request, but on any subsequent requests I send, I am not able to retrieve it. I am creating a new connection each time I send a request. So, something might be preventing the request body from being reteieved. I am not sure why.

A: 

Try replacing

request.ContentType = "multipart/form-data";

with

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

or check this SO answer for code which works with multipart/formdata.

Mikael Svenson
I tried that approach. It didn't work. I made an edit to the original question at the end. That might help clarifying further. Thanks.
venividivamos
Then I would fire up Fiddler and see what goes over the wire for the first and second request, and compare them.
Mikael Svenson