Question

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.

Was it helpful?

Solution

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 by 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();
    }
}

OTHER TIPS

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.

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top