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 + '¶m2=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.
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 callCloseif you’ve already got ausingstatement for the response stream: