Sunday 15 July 2012

c# - WebResponse Try Catch -


I am using WebRequest / WebResponse to capture some remote HTML. When my code hits an error with a remote server, the exception is thrown.

Here is an example of my code:

  HttpWebRequest request = (HttpWebRequest) WebRequest.Create (URL); Request.UserAgent = "Mozilla / 5.0 (Windows NT 6.0) Apple WebKat / 535.1 (KHML, like GECO) Chrome / 13.0.782.112 Safari / 535.1"; WebResponse response = Request GetResponse (); String HTML = Response Toasting ();   

Exception "WebSites response = request. Getspons ();" Throwing on "

If I try to wrap it in the block, then the" reaction "is underlined below and says that the name 'reaction' is not present in the current context. There is an example:

  try {WebResponse response = request.GetResponse ();} hold {} string HTML = response.tostring ();   

How do I check "feedback" while being able to use it?

WebResponse response = null; try {response = request.GetResponse ();} hold {} string HTML = response.tostring ();

You are receiving a compile-time error because your variable was declared in the scope of try..catch and local variables can only be transmitted within their scope. Essentially marked by opening and closing brackets, the scope of everything between the bracket .

Specifically, the exception you receive when you request.GetResponse (); .

This means that you can safely bring the variable out of the scope of try..catch and it can reach the scope of the law and still make any exception Can be caught which can be thrown as a result you are requesting . Gatorresons ();

No comments:

Post a Comment