输入数据到网站(不是登录屏幕)

人气:236 发布:2022-09-22 标签: http c# visual-studio HTTP web-dev

问题描述

所以我试图使用网络应用程序asp.net输入数据字符串,但我试图找出如何。所有我发现是登录网站,到目前为止,我没有运气通过修改我发现的代码以满足我的需要让它工作 所以这里是什么我到目前为止:

 string entry = bookEntry.Text;  //用于构建整个输入 StringBuilder sb = new StringBuilder();   //在每次读操作中使用 byte [] buf = new byte [8192];   //准备我们要求的网页 HttpWebRequest request =(HttpWebRequest) WebRequest.Create(http://www.abebooks.com/servlet/ SearchEntry);  StreamReader responseRead = new StreamReader( request.GetResponse()。GetResponseStream());   string responseData = responseRead.ReadToEnd();  responseRead.Close();   string postData =  String.Format(& tn2 = {1}& sortBy = {2}& findBook-advancedSearch = Start%Search,条目,2);   CookieContainer cookies = new CookieContainer();   request = WebRequest.Create(http://www.abebooks.com/servlet/SearchEntry)as HttpWebRequest;  request.Method =POST;  request.ContentType =application / x-www-form-urlencoded;  request.CookieContainer = cookies;   StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());  requestWriter.Write(postData);  requestWriter.Close();  request.GetResponse()。Close();   responseData = responseRead.ReadToEnd();  responseRead.Close();   Response.Write(responseData); 

我试图输入数据的网站是 www.abebooks.com [ ^ ] 感谢先进的

解决方案

您的代码有多个问题。我建议你阅读有关屏幕抓取的内容,并对如何处理http请求/响应有一个大致的了解。此外,你应该得到提琴手( http://fiddler2.com/home [ ^ ]),这是一个工具,可让您查看Web浏览器和Web服务器之间的通信。一旦你知道正在传达什么,你就可以开始模仿代码中的过程。 您正在调用的页面实际上是重定向到另一个页面并为搜索传递查询字符串信息。所以,你实际上不必做POST。相反,您可以进行GET调用并在查询字符串中传递数据。通常最好遵循真实用户在尝试屏幕废弃时所做的步骤,但有时您只需要结果。 我的下面的代码简化了流程,并不是最好的方法,但它会为你提供你想要的结果。

  string  searchText =  < span class =code-string> steven king;    //  注意此网址正在调用SearchResults    string  url =  string  .Format(  http://www.abebooks.com/servlet/SearchResults?an= {0}& sortby = {1},searchText, 2 );    //  准备请求  CookieContainer cookies =  new  CookieContainer();  HttpWebRequest request =(HttpWebRequest)WebRequest.Create(url);  HttpWebResponse response =  null ;    //  设置标题值  request.Method = < span class =code-string>  GET;  request.CookieContainer = cookies;    //  获取响应数据  response =(HttpWebResponse) request.GetResponse();  request =  null ;    string  responseData =  new  StreamReader(response.GetResponseStream())。ReadToEnd ();  response.Close();  response =  null ;    //  显示结果  Response.Write(responseData) ; 

hi, so im trying to enter strings of data using a web application asp.net but im trying to find out how. all i find is loging in websites and so far i had no luck trying to get it to work by modifying the code i found to suit my needs so here is what i have so far:

string entry = bookEntry.Text;
            // used to build entire input
            StringBuilder sb = new StringBuilder();

            // used on each read operation
            byte[] buf = new byte[8192];

            // prepare the web page we will be asking for
            HttpWebRequest request = (HttpWebRequest)
                WebRequest.Create("http://www.abebooks.com/servlet/SearchEntry");
            StreamReader responseRead = new StreamReader(
                request.GetResponse().GetResponseStream());

            string responseData = responseRead.ReadToEnd();
            responseRead.Close();

            string postData =
                String.Format(
                "&tn2={1}&sortBy={2}&findBook-advancedSearch=Start%Search",
                entry, 2);

            CookieContainer cookies = new CookieContainer();

            request = WebRequest.Create("http://www.abebooks.com/servlet/SearchEntry") as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookies;

            StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
            requestWriter.Write(postData);
            requestWriter.Close();
            request.GetResponse().Close();

            responseData = responseRead.ReadToEnd();
            responseRead.Close();

            Response.Write(responseData);

the website im trying to enter data to is www.abebooks.com[^] thanks in advanced

解决方案

Your code has multiple issues. I suggest you read up on screen-scraping and gain a general understanding on how http requests/responses are processed. Also, you should get fiddler(http://fiddler2.com/home[^]), this is a tool that lets you view the communication between the web browser and the web server. Once you know what's being communicated you can start to mimic the process in code. The page you are calling is actually redirecting to another page and passing a querystring for the search information. So, you don't actually have to do a POST. Instead you can make a GET call and pass your data in the querystring. It's usually better to follow the steps a real user would make when attempting to screen-scrap but sometimes you just need results. My code below has simplified the process and is not the best way to go about this but it will get you the results you want.

 string searchText = "steven king";

// notice this url is calling SearchResults
string url = string.Format("http://www.abebooks.com/servlet/SearchResults?an={0}&sortby={1}",searchText,2);
            
// prepare request
CookieContainer cookies = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = null;

// set header values
request.Method = "GET";
request.CookieContainer = cookies;

// get response data
response = (HttpWebResponse)request.GetResponse();
request = null;
            
string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
response = null;

// show results
Response.Write(responseData);

362