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

C# Webresponse Error with special server

Hi,

I have written a short code snippet, that schould response the html source code of a website. Normally it wokrs fine I have testet it on some websites, but at thegreek.com I get an error.
here is the code:
Expand|Select|Wrap|Line Numbers
  1. public static string getSourceCode(string url)
  2.         {
  3.             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  4.             HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  5.             StreamReader sr = new StreamReader(resp.GetResponseStream());
  6.             string SourceCode = sr.ReadToEnd();
  7.             sr.Close();
  8.             resp.Close();
  9.             return SourceCode;
  10.         }
  11.  
But when the url is http://thegreek.com I get the following error:
Expand|Select|Wrap|Line Numbers
  1. System.Net.WebException wurde nicht behandelt.
  2.   Message=Die zugrunde liegende Verbindung wurde geschlossen: Unbekannter Fehler beim Empfangen..
  3.   Source=System
  4.   StackTrace:
  5.        bei System.Net.HttpWebRequest.GetResponse()
  6.        bei Scraper_TheGreek.WebWorker.getSourceCode(String url) in C:\Users\Admin\Desktop\Scraper_TheGreek\Scraper_TheGreek\WebWorker.cs:Zeile 16.
  7.        bei Scraper_TheGreek.Program.Main(String[] args) in C:\Users\Admin\Desktop\Scraper_TheGreek\Scraper_TheGreek\Program.cs:Zeile 16.
  8.        bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
  9.        bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
  10.        bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  11.        bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  12.        bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
  13.        bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  14.        bei System.Threading.ThreadHelper.ThreadStart()
  15.   InnerException: System.IO.IOException
  16.        Message=Von der Übertragungsverbindung können keine Daten gelesen werden: Eine vorhandene Verbindung wurde vom Remotehost geschlossen.
  17.        Source=System
  18.        StackTrace:
  19.             bei System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
  20.             bei System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
  21.             bei System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
  22.        InnerException: System.Net.Sockets.SocketException
  23.             Message=Eine vorhandene Verbindung wurde vom Remotehost geschlossen
  24.             Source=System
  25.             ErrorCode=10054
  26.             NativeErrorCode=10054
  27.             StackTrace:
  28.                  bei System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
  29.                  bei System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
  30.             InnerException: 
  31.  
Does anybody know, what the mistake is?
In the browser it is now problem to diplay the website, it is possible with Firefox and Internet Explorer
Mar 5 '11 #1
1 3395
Aimee Bailey
197 Expert 100+
the problem is you are not passing the request a useragent string. simply ammend your code to the following (for example):

Expand|Select|Wrap|Line Numbers
  1. public static string getSourceCode(string url)
  2. {
  3.     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  4.     req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.21 (KHTML, like Gecko) Chrome/11.0.682.0 Safari/534.21";
  5.     HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  6.     StreamReader sr = new StreamReader(resp.GetResponseStream());
  7.     string SourceCode = sr.ReadToEnd();
  8.     sr.Close();
  9.     resp.Close();
  10.     return SourceCode;
  11. }
  12.  
Mar 5 '11 #2

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

Similar topics

0
by: microsoft | last post by:
Hi People, when I try to modify an active directory user programatically, I receive the following exception: The server is unwilling to process the request Reading the microsoft web site, I...
1
by: Jacob | last post by:
Please help figure what I am doing wrong in this code. All I am trying to do is send come data to an aspx page and get some data back. I watched my variable in debug mode and can tell that I am...
1
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have developed a couple of web pages. Those are running fine on my IIS server. Now, i have created a couple of pages for the cellphones. Can i use the same IIS server or is it a special...
2
by: hfk0 | last post by:
Hello, I'm a newbie here and was wondering anyone could help me with this. I have a simple ASP.NET 2 web application running perfectly fine with IIS and SQLServerExpress installed locally on...
0
by: dmlinliverpool | last post by:
I am running VS.net 2005 Express and Sql Server 2005 Express. The DB and VS are both on the same PC. I cannot connect to a database from within VS. In Database Explorer I click Connect To...
1
by: Atia Amin | last post by:
Hi, I am a new member. Hello to every one. I am new in ASP.NET area. I wrote an ASP.NET web application which was running ok with my old laptop. Currently I have given a new laptop. Now I copied...
1
by: steve1rm | last post by:
Hello, I have a problem trying to connect a remote sql server. The error: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may...
0
by: snowman | last post by:
I have vb and sql server express 2005 installed locally. I cannot add a data connection in vb express. I keep getting the error "An error has occured while establishing......26 - Error Locating...
4
by: jobs | last post by:
Works great on my client when I test from vs.net 2005. Howevever, when I deploy to the web server, I get this error: remotely and local from the web server. An error has occurred while...
2
by: misogsk | last post by:
Hi All, i'm using Sql Server 2005 Standard, IIS 6.0 an Windows 2003 Server. I deployed my project on IIS (Default web site) everything goes well, but i created new web site on IIS called MCSite and...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.