locked
Problem to get Stream with WebRequest RRS feed

  • Question

  • Hi,

    I'm developping a web service using a dll that contains a method wich download file from uri. My dll works fine before integrated it into my web service. But now, I have an exception when I try to get response (stream) from WebRequest object.

    "Impossible to connect to remote server".

     WebRequest req = WebRequest.Create(uri);
    
    
    
    //Here, is the problem
    
     WebResponse response = req.GetResponse();
    
    Stream fStream = response.GetResponseStream();
    
    
    Thanks
    Friday, July 9, 2010 8:44 AM

Answers

  • It was a problem in the server. Now, its works ! Thanks
    • Marked as answer by Amine ZEMZEMI Friday, July 9, 2010 12:54 PM
    Friday, July 9, 2010 12:54 PM

All replies

  • first, you're using the wrong classes. You need to use
    HttpWebRequest and HttpWebResponse. Example:

    HttpWebRequest req = WebRequest.Create(sAddressTime);
    HttpWebResponse result = req.GetResponse();

    The HttpWebRequest and HttpWebResponse classes are derived from WebRequest
    and WebResponse, but more specific to Http. In fact, when you use an HTTP
    URL with WebRequest.Create, you get an HttpWebRequest back, and when you
    send an HttpWebRequest, you get an HttpWebResponse back.

     

    try this....

    One of the first things you might want to do is check the Response. Here's a
    little function I wrote that parses a string containing Response information
    from an HttpWebResponse:

    /// <summary>
    /// Retrieves Response Information from the Response
    /// </summary>
    /// <param name="Response">Formatted string containing Response
    Information</param>
    /// <returns>Formatted string containing Response Information</returns>
    public static string GetResponseInfo(HttpWebResponse Response)
    {
    try
    {
    StringBuilder sb = new StringBuilder(Response.ResponseUri.ToString());
    sb.Append(Environment.NewLine + "Status Code: " +
    Response.StatusCode.ToString());
    sb.Append(Environment.NewLine + "Status Description: " +
    Response.StatusDescription);
    sb.Append(Environment.NewLine + "Content Length: " +
    Response.ContentLength.ToString());
    return sb.ToString();
    }
    catch (Exception ex)
    {
    //whatever you want to do to handle the exception
    }
    }

     

    Friday, July 9, 2010 11:15 AM
  • It was a problem in the server. Now, its works ! Thanks
    • Marked as answer by Amine ZEMZEMI Friday, July 9, 2010 12:54 PM
    Friday, July 9, 2010 12:54 PM