473,395 Members | 1,689 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,395 software developers and data experts.

WebResponse.GetResponseStream returns incomplete stream

2
Hi,

I have some C# code for a console application that was correctly grabbing pages until recently were the data is now incomplete. I am needing to grab all information including sponsored links for a url similar to: "http://search.live.com/results.aspx?q=sony+bmg" All sponsored ads on the site are contained in one of two <div> tags: <div id="at"> or <div id="ar"> when I save the output to a file and search for these tags they are not found. If I use a browser and go to the above link and view the source then I easily find the <div> tags containing the sponsored links. This was working for the past couple of months and only recently it stopped working and so I don't believe that the problem is related to my code. Below is a simplified version of my program and illustrates the same problem.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6.  
  7. namespace WebReader
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string url = "http://search.live.com/results.aspx?q=sony+bmg";
  14.             Uri uri = new Uri(url);
  15.  
  16.             WebRequest req = WebRequest.Create(uri);
  17.             WebResponse resp = req.GetResponse();
  18.             Stream stream = resp.GetResponseStream();
  19.             StreamReader sr = new StreamReader(stream);
  20.             string s = sr.ReadToEnd();
  21.  
  22.             System.IO.StreamWriter myFile = new System.IO.StreamWriter("c:\\WebRead.txt");
  23.             myFile.Write(s);
  24.             myFile.Close();
  25.         }
  26.     }
  27. }
  28.  
Things I have Tried:
  • I have tried buffering the response in MemorySteam with the same results


Any suggestions that you may have on how to get the entire page returned so I can parse it would be appreciated.
Feb 15 '08 #1
1 5465
vito16
2
I have solved the problem, it looks like live.com is now requiring the useragent information before returning the sponsored ads. I simply had to add one line which casted my WebRequest object to an HttpWebRequest and assigned the user agent. Below is the updated code which resolved my problem.


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6.  
  7. namespace WebReader
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string url = "http://search.live.com/results.aspx?q=sony+bmg";
  14.             Uri uri = new Uri(url);
  15.  
  16.             WebRequest req = WebRequest.Create(uri);
  17.             ((HttpWebRequest)req).UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows 5.1;";
  18.             WebResponse resp = req.GetResponse();
  19.             Stream stream = resp.GetResponseStream();
  20.             StreamReader sr = new StreamReader(stream);
  21.             string s = sr.ReadToEnd();
  22.  
  23.             System.IO.StreamWriter myFile = new System.IO.StreamWriter("c:\\WebRead.txt");
  24.             myFile.Write(s);
  25.             myFile.Close();
  26.         }
  27.     }
  28. }
  29.  
Feb 15 '08 #2

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

Similar topics

2
by: ScottO | last post by:
How can I upload a file to a ASP.NET page without having the stream eating up memory without using a 3rd party component? How can I download a file from a different webpage without eating up...
0
by: Johann Blake | last post by:
In my need to decode a JPEG 2000 file, I discovered like many that there was no functionality for this in the .NET Framework. Instead of forking out a pile of cash to do this, I came up with the...
2
by: Jeff G. | last post by:
Hello everyone, I have read through the newsgroups (thank God for 'em!) extensively looking for an example of how to pass a file (PDF) from a webresponse stream down to a web client. Here's the...
0
by: Tony Archer | last post by:
Here's my problem... When I use this code to hit the site in question I'm redirected to another SSL login page. If processed successfully the page creates a cookie and then redirects you to the...
10
by: Chris Coddington | last post by:
I make a request, stuff it into a stringbuffer, then append it to a textbox. It works on many sites (e.g. yahoo.com), but not on many others (e.g. amazon.com). Any insight that could be provided...
4
by: Savas Ates | last post by:
I have a vb.net web application. I want to post some variables to another web page and take some values back and process them. This is codes in my target url. I should post "langpair" cariable...
9
by: ThePants | last post by:
Hi, given the following code, I've been successful in grabbing pages for parsing, but for a certain page template (containing a particular piece of code) the stream always ends right after that...
4
by: CindyH | last post by:
Hi I'm trying to use webrequest - webresponse to post a stream. I have set up a simple test with one aspx form holding the post code and trying to get another aspx form to receive the post on...
0
by: deeps1512 | last post by:
hi All, I have written the following code to get a webresponse. XmlDocument myxmlDoc1 = new XmlDocument(); myxmlDoc1.LoadXml(BkXMLRQ); string s = myxmlDoc1.OuterXml;...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.