Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
HttpRes take initialize in section Try, but i can't use HttpRes in section Catch with the same amount in Try

C#
   try
        {
            //Code Example
            string url = "http://www." + TextBox1.Text;
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
            httpReq.AllowAutoRedirect = false;

            HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
          
                Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
           
            // Close the response.
            httpRes.Close();
        }

catch
        {

            Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
               
        }
Posted
Updated 15-Jun-12 21:05pm
v2

Hi,

Look into you code carefully. You are not closing your try block.

C#
try
        {
            //Code Example
            string url = "http://www." + TextBox1.Text;
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
            httpReq.AllowAutoRedirect = false;
 
            HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
          
                Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
           
            // Close the response.
            httpRes.Close();
         }//This was not there you missed.
catch
        {
 
            Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
               
        }
 
Share this answer
 
Comments
me64 16-Jun-12 3:01am    
i close try block in master source code
my problem is using HttpRes in section catch
Just Declare The httpRes Before Try Block
C#
 HttpWebResponse httpRes;
try
        {
            //Code Example
            string url = "http://www." + TextBox1.Text;
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
            httpReq.AllowAutoRedirect = false;
 
            httpRes = (HttpWebResponse)httpReq.GetResponse();
          
                Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
           
            // Close the response.
            httpRes.Close();
        }
 
catch
        {
 
            Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
               
        }
 
Share this answer
 
Comments
me64 16-Jun-12 3:26am    
Error: Use of unassigned local variable 'httpRes'
me64 16-Jun-12 3:38am    
i want using value https take was in try block
u declare your httpRes variable before the try like this

C#
HttpWebResponse httpRes;
try
        {
            //Code Example
            string url = "http://www." + TextBox1.Text;
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
            httpReq.AllowAutoRedirect = false;
 
            httpRes = (HttpWebResponse)httpReq.GetResponse();
          
                Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
           
            // Close the response.
            httpRes.Close();
         }//This was not there you missed.
catch
        {
 
            Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
               
        } 
 
Share this answer
 
Comments
me64 16-Jun-12 3:27am    
Error: Use of unassigned local variable 'httpRes'
me64 16-Jun-12 3:38am    
i want using value https take was in try block
I have seen the solution here already.
Just declare the HttpWebResponse[^] outside the try-catch block[^].
If you do you must be aware the it could have a null reference in your catch block though.
C#
HttpWebResponse httpRes;
try
{
    // Code might crash here.
    httpRes = (HttpWebResponse)httpReq.GetResponse();
    // Etc.
    }
catch
{
    if (httpRes != null)
    {
        Label1.Text = httpRes.StatusDescription; // No need to cast here.
    }
    else
    {
        // Do something else.
    }
}
Hope it helps :)
 
Share this answer
 
v2
Comments
me64 16-Jun-12 3:52am    
With this method, if have address website problem, give Error massage in runtime
Sander Rossel 16-Jun-12 4:57am    
Yes, I understand. But you must see that if the HttpWebResponse was not properly initialized for whatever reason you cannot use it.
The best you can do in that case is show something like a more general error message.
me64 16-Jun-12 5:09am    
If you give an example for me, excellent Is
Sander Rossel 16-Jun-12 5:30am    
I gave you an example... Read my answer.
Just replace // Do something else with Label1.Text = "An unexpected error has occured." or something similar.
me64 16-Jun-12 5:36am    
But With this method, if have address website problem, give Error massage in runtime

I need to review When the address (httpRes) field is empty or null
just Declare HttpWebResponse httpRes globally
C#
public partial class _Default : System.Web.UI.Page
{
//DECLARE HERE
HttpWebResponse httpRes;

   protected void Page_Load(object sender, EventArgs e)
    {
     try
      {
       // Code might crash here.
       httpRes = (HttpWebResponse)httpReq.GetResponse();
       // Etc.
      }
    catch
      {
       if (httpRes != null)
         Label1.Text = httpRes.StatusDescription; // No need to cast here.
      }
    }
 }
 
Share this answer
 
v2
Comments
Sander Rossel 16-Jun-12 4:58am    
Why globally if you only need it locally? You should keep the scope of your objects as small as possible.
Making it global makes little sense...
Sander Rossel 16-Jun-12 5:00am    
By the way, I edited your answer for readability.
I can now also see you copied my code and edited it...
me64 16-Jun-12 5:14am    
Naerling,
If you give an example for me,excellent Is
me64 16-Jun-12 15:58pm    
what i can review When the address (httpRes) field is empty or null
because in runtime give error massege

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