views:

1148

answers:

3

This is driving me nuts and I can't figure out where I am dropping the ball. I've followed a few examples found via the googlemonsta to no avail. Any pointer to where I goofed would be greatly apperciated.

        var writer = new StringWriter();
        param = "location=" + Server.UrlEncode(param);
        byte[] paramStream = Encoding.ASCII.GetBytes(param + "&param2=value");
        var URL = "http://www.somesite.com";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; sv-SE; rv:1.9.1b2) Gecko/20081201 Firefox/3.1b2";
        request.ContentLength = paramStream.Length;
        using( var stream = request.GetRequestStream())
        {
            stream.Write(paramStream, 0, paramStream.Length);
        }

        var response = request.GetResponse();

        string result;
        using (var sr = new StreamReader(response.GetResponseStream()))
        {
            result = sr.ReadToEnd();
        }

Thanks!

EDIT: As far as I can tell its hitting the site (i'm getting html back) but the params aren't pushed over. I'm basically getting where the values would appear had it been successful. I've tried removing the first & but didnt get anywhere.

EDIT: Edited code to reflect changes.

+3  A: 

Possibly get rid of the & from the start of the first parameter? Other than that it basically looks okay. (Check the parameter names in your real code - where you've got "paramater" in the sample it should almost certainly be "parameter" - but we don't know what your real code looks like or what the real site expects.)

Please give more information about what's actually happening. We know it doesn't work, but there are a lot of different possible failure modes :)

One extra thought occurs - you haven't specified the content length. I'm not sure whether this is filled in automatically be WebRequest. It would be worth using WireShark to check whether or not it's present in the outbound request.

Just as a general point of practice, you should dispose of the WebResponse, and you don't need to call Close if you've already got a using statement for the response stream:

string result;
using (WebResponse response = request.GetResponse())
{
    using (var sr = new StreamReader(response.GetResponseStream()))
    {
        result = sr.ReadToEnd();
    }
}
Jon Skeet
Thanks for the response Jon, quick question though: what would the length be? If I use paramStream + URL then it errors out b/c I didn't write all, if I use paramStream OR URL then it fails due to exceeding the length. Thanks!
Chance
Paramstream.Length should be the right size.
Jon Skeet
Chance
+1  A: 

Are you sure you have all the values neccessary for the post? I once had a case where there was a hidden input field on the form that was something like:

<input name="action" type="hidden" id="action" value="login">

and I had to supply that as a param as:

&action=login

Make sure you're not missing anything from the form is what I'm saying...

EDIT: One more thing: I just looked at my code again where I've done this, and noticed that I also had this line in there:

request.ContentLength = bytes.Length;

Not sure if you need that, but I noticed that you weren't setting the length.

BFree
Cool thanks - added it to no avail though. Just waiting around for wireshark to see whats happening under the hood.
Chance
A: 

It is not something as simple as a carriage-return/new-line after the parameters is it? Looking at some docs on HTTP on the internets, you apparently need a blank line afterwards.

(I would suggest telneting to the web server manually and pasting your request.)

Paul Ruane