我正在尝试以编程方式在该网站上搜索某个项目。
以下对我不起作用。响应实际上是一个错误页面,而不是搜索结果页面。
请帮助。
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();
}
答案 0 :(得分:0)
您可能没有像网站期望的那样发送完全的请求。我会做以下事情:
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?
答案 1 :(得分:0)
为什么在简单的时候编写这么多代码:
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);
}
}
}