473,379 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Reading Url source with c# problem

Hi

I am trying to read source of a web page (ASP) to grab pictures. I am coding in C# and I can succesfully do it with the following code for html pages.

WebRequest url = WebRequest.Create(url2);
WebResponse response = url.GetResponse();
StreamReader input = new StreamReader(response.GetResponseStream());
recieve = input.ReadToEnd().ToString();


input.Close();
response.Close();

But it fails in ASP pages. as an example...when url2=http://www.pearsoned.co.uk/bookshop/detail.asp?item=305606 it only grabs the source for http://www.pearsoned.co.uk/bookshop/detail.asp and since function did not completed Icannot see the location for the picture"9780201882605.jpg". However the location of the pic is visible in IExplorer->View->Source.

any help will be apreciated ...
Mar 21 '08 #1
5 2233
balabaster
797 Expert 512MB
Are you sure your URL2 variable has the full address in it?

url2=http://xx.com/item.asp?no=1701

I just ran your code against a website I wrote (well actually, I translated it to VB first, but it's the same code) that uses querystrings and I substituted the address in place of URL2 in your code and it worked just fine for me.
VB
Expand|Select|Wrap|Line Numbers
  1. Dim url As WebRequest = WebRequest.Create("http://localhost/OnCall/OnCallView.aspx?loc=23")
  2. Dim response As WebResponse = url.GetResponse()
  3. Dim input As StreamReader = New StreamReader(response.GetResponseStream())
  4. Dim receive As String = input.ReadToEnd().ToString()
  5. System.IO.File.WriteAllText("C:\Test.htm", receive)
C#
Expand|Select|Wrap|Line Numbers
  1. WebRequest url = WebRequest.Create(http://localhost/OnCall/OnCallView.aspx?loc=23);
  2. WebResponse response = url.GetResponse();
  3. StreamReader input = new StreamReader(response.GetResponseStream());
  4. String receive = input.ReadToEnd().ToString();
  5. System.IO.File.WriteAllText("C:\\Test2.htm", receive);
Both work just fine at my end...
Mar 21 '08 #2
well thanks for the quick answer. But NOW it is working for me too.. But I need to try the code at work for our local pages on monday. I am confused :)
Mar 21 '08 #3
hello again

No it is not working in my example. But the code works. The thing that I am working is local so I have no chance to give you the link. But I can tell you the problem.

In my example threre is an item_search.asp with few checkbox options. After submit button pressed (with item no in textbox) it returns item.asp?no=1701. If I enter direct link as url2=http://xx.com/item.asp?no=1701 it returns me the source of http://xx.com/item_search.asp. (Guess there are sub functions running and since i didnt enter any it gives me the home page)

I did use another solution as



HTMLDocument myDoc = new HTMLDocumentClass();
myDoc = (HTMLDocument) axWebBrowser1.Document;
HTMLDocument myDoc2 = new HTMLDocumentClass();
HTMLInputElement otxtSearchBox = (HTMLInputElement) myDoc.all.item("item_no", 0);

otxtSearchBox.value =itembox.Text;
otxtSearchBox.focus();
SendKeys.Send("{ENTER}");
System.IO.StreamWriter str = null;

if ( ( (int)axWebBrowser1.ReadyState >= 4) & ( ! axWebBrowser1.Busy ))
{
str = new StreamWriter("C:\\saved.txt");
str.Write( myDoc2.documentElement.innerHTML) ;
}

but guess.......did not work...I am completely out of ideas. AND please share any ideas if you have..
Mar 27 '08 #4
Plater
7,872 Expert 4TB
Well, my first suggestion is to use HttpWebReques over WebRequest (if you have it, might be new in .NET2.0).

Then I guess I would try to simulate the selection system that goes on, possibly with a series of HttpWebRequest(or WebRequest).
Mar 27 '08 #5
balabaster
797 Expert 512MB
hello again

No it is not working in my example. But the code works. The thing that I am working is local so I have no chance to give you the link. But I can tell you the problem.

In my example threre is an item_search.asp with few checkbox options. After submit button pressed (with item no in textbox) it returns item.asp?no=1701. If I enter direct link as url2=http://xx.com/item.asp?no=1701 it returns me the source of http://xx.com/item_search.asp. (Guess there are sub functions running and since i didnt enter any it gives me the home page)

I did use another solution as



HTMLDocument myDoc = new HTMLDocumentClass();
myDoc = (HTMLDocument) axWebBrowser1.Document;
HTMLDocument myDoc2 = new HTMLDocumentClass();
HTMLInputElement otxtSearchBox = (HTMLInputElement) myDoc.all.item("item_no", 0);

otxtSearchBox.value =itembox.Text;
otxtSearchBox.focus();
SendKeys.Send("{ENTER}");
System.IO.StreamWriter str = null;

if ( ( (int)axWebBrowser1.ReadyState >= 4) & ( ! axWebBrowser1.Busy ))
{
str = new StreamWriter("C:\\saved.txt");
str.Write( myDoc2.documentElement.innerHTML) ;
}

but guess.......did not work...I am completely out of ideas. AND please share any ideas if you have..
Okay - what are you trying to achieve?

My original understanding is that given a URL with parameter extensions on the address, you wish to be able to parse the source code and provide a list of images on that page. Given that the parameter extensions may change, the list of images within that page may change too.

For instance pictures for the address with extension "?no=1701" would be different pictures for the address with extension "?no=1702".

Also, from my understanding of what you wrote above: If you add the address directly by: URL2 = "item.asp?no=1701" it works, but if you populate URL2 dynamically it's not working.

If you put these addresses into Internet Explorer directly, the pictures load and are displayed correctly. If you view the source from Internet Explorer, can you tell me if the picture locations are fully qualified paths to locations of the pictures on the remote host or are they relative to the path of the asp page you are viewing? i.e. do they appear as http://xx.com/images/image1.gif or ./images/image1.gif? If these picture files are relative, you will obviously need to add the address in your application. I suspect that they're fully qualified paths or it wouldn't work when you hard code the address. What I suspect is that the address isn't being populated correctly when you assign it dynamically.

URL2 = "http://xx.com/item.asp?no="& CurrentItem

Get the page for URL2... is this how you're loading URL2 or something?
Mar 27 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
13
by: mloichate | last post by:
I must read a very heavy-weight text plain file (usually .txt extension) )and replace a given character with another given character in all text inside the file. My application was working pretty...
0
by: Manfred Braun | last post by:
Hi All, I have a problem reading queue-messages async. My QueueReader has a Start() and a Stop() method and if my app starts, it calls Start(). The problem is, that there are possibly several...
1
by: svijay | last post by:
hi I have got a strange problem. May I know any solution for this. Here is the detailed description about the problem We have got a mainframe system and also production and development...
1
by: hzgt9b | last post by:
(FYI, using VB .NET 2003) Can someone help me with this... I'm trying to read in an XML file... it appears to work in that the DataSet ReadXML method dose not fail and then I am able to access the...
2
by: bhreddy | last post by:
Hi All, Can someone help me out how can I resolve the error "0xC0000005: Access violation reading location 0x513112f4"? Steps I followed... 1. I ran the application at DOS prompt 2. After...
3
by: Brad | last post by:
I'm having a problem reading data from an Excel file into a dataset. Can anybody give me an idea of what's happening? I've included the problematic source and the error message to the end of this...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
2
by: tshad | last post by:
I have a program that is reading a csv file into a dataset. I want it to read the 1st line as data. But it ignores it. I have the Connection set up as: OleDbConnection csvConnection = new...
1
by: sachinkale123 | last post by:
Hi, I am reading excel file and reading values from that I am using provider As : "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Filename + ";Extended Properties=\"Excel 8.0;Hdr=No;IMEX=1\"";...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.