Click here to Skip to main content
15,888,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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
Posted
Updated 13-Dec-13 6:05am
v2
Comments
bowlturner 13-Dec-13 13:48pm    
I'm going to have to ask you clarify what it is you are actually trying to do.
slayasty 13-Dec-13 13:59pm    
what i am trying to do is using my web application to enter the book title or isbn number, select the criteria i need and hit the search button. the problem is how to input this data from my web application to this website

1 solution

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.

C#
 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);
 
Share this answer
 
Comments
bowlturner 13-Dec-13 15:54pm    
Right, don't need to 'use' the search page, just create the link.

ie. http://www.abebooks.com/servlet/SearchResults?an=rollings&sts=t replace 'rollings' with say 'Baldacci' and you'll get books by baldacci.
slayasty 13-Dec-13 18:56pm    
i see. thanks alot i shall try that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900