httpwebresponse doesnt work
I'm trying to programmatically search for an item on the said website.
Following does not work for me. Response is actually an error page, instead of the search result page.
Pls help.
string sUrl = "http://www.arrownac.com/";
string sUrl1 = "http://app.arrownac.com/aws/pg_webc?s=P";
HttpWebRequest owebreq = (HttpWebRequest)WebRequest.Create(sUrl1);
owebreq.Referer = sUrl;
ASCIIEncoding encoding = new ASCIIEncoding();
string postdata = "search_token=" + "743C083102JPTR";
byte[] data = encoding.GetBytes(postdata);
owebreq.ContentType = "text/html";
//"application/x-www-form-urlencoded";
owebreq.Method = "POST";
owebreq.ContentLength = data.Length;
Stream newStream = owebreq.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse owebresp = (HttpWebResponse)owebreq.GetResponse();
string sResult开发者_运维技巧 = string.Empty;
using (StreamReader sr = new StreamReader(owebresp.GetResponseStream()))
{
sResult = sr.ReadToEnd();
sr.Close();
}
You are probably not sending the request exactly as the website expects it. I would do the following:
1) use firefox. 2) install firebug plugin 3) use firefox to execute your scenario. 4) Look at the actual requests sent from firefox (use the firebug plugin for that) 5) Now, duplicate the exact same requests, down to the user-agent,cookies and request headers in code. 6) profit?
Why writing so much code when it can be simple:
class Program
{
static void Main()
{
using (var client = new WebClient())
{
var referer = "http://www.arrownac.com/";
client.Headers[HttpRequestHeader.Referer] = referer;
var valuesToPost = new NameValueCollection
{
{ "search_token", "743C083102JPTR" },
};
var url = "http://app.arrownac.com/aws/pg_webc?s=P";
var result = client.UploadValues(url, valuesToPost);
var resultString = Encoding.Default.GetString(result);
Console.WriteLine(resultString);
}
}
}
精彩评论