WebRequest is breaking me in 2.0


Trying to get my code ported to RTM and I've run into a nasty snag.

Here's the code:

public Image RetrieveImage(Uri path)

{

WebResponse Response = null;

Image image = null;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);

request.Credentials = CredentialCache.DefaultCredentials;

try

{

Response = request.GetResponse();

if (Response.ContentLength > 0)

{

image = Image.FromStream(Response.GetResponseStream());

}

}

catch (Exception e)

{

ExceptionManager.HandleException(e);

}

finally

{

if (Response != null)

{

Response.Close();

}

}

return(image);

}

This code has worked for months with no problems.  My unit tests with this code pass successfully.  However, when this code is called from within WinForms, it fails with the following exception:

{System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at SuperVillain.NetworkFetcher.RetrieveImage(Uri path) in E:\users\Ed Draper\My Documents\Visual Studio\Projects\ElectricEye\ElectricEye\networkfetcher.cs:line 36}

I thought this was related to CAS, so I up the trust level to FullTrust and even inserted a Demand decoration on the method:

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]

What's up

 



Answer this question

WebRequest is breaking me in 2.0

  • CEisen

    It is very suspicious that One Care is the issue. I can't belive One Care would make LISTDirectory not work but the ListDirectoryDetails works. I am not sure exactly one care does, but since it is in Beta2, it may have some quirky bugs.
    In any case I am glad it is working for you now.


  • lmaster


    Okay... I've got it nailed.  It looks like it was the Windows OneCare beta.

    I uninstalled it, and it my app worked.  A couple of questions:

    Why did my unit test work
    Why didn'y OneCare prompt me to unblock the network connectivity, like it does with every other app

  • James_Lin

    Can you use tracing and send me a trace file
    I think there is another process using the port the HttpRequest is using.
    http://blogs.msdn.com/dgorti/archive/2005/09/18/471003.aspx
    describes how to get a log.

    In the mean time you can try binding explicitly to a different port
    http://blogs.msdn.com/malarch/archive/2005/09/13/466664.aspx

    Bind to a port > 5000 and see if the problem still occurs.

    Also send us the netstat -a output


  • Heath Stewart - MSFT

    Something Interesting is going on with the machine you are using
    I used the following C# code in a WinForms app and things work fine.

    I looked at the trace file you sent me and I did not see the particular exception you mentioned, although I have seen a ton of "Unable to connect to the server"
    exceptions. I would recommend that you use IE to test if the URLs you are using are reachable from the machine you are testing with

    DO you have a firewall or any third party antivirus or anyother interesting
    software on this machine



    using System;

    using System.Net;

    using System.Net.Sockets;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace WindowsApplication1

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    public Image RetrieveImage(Uri path)

    {

    WebResponse Response = null;

    Image image = null;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);

    request.Credentials = CredentialCache.DefaultCredentials;

    try

    {

    Response = request.GetResponse();

    if (Response.ContentLength > 0)

    {

    image = Image.FromStream(Response.GetResponseStream());

    }

    }

    catch (Exception e)

    {

    MessageBox.Show(e.ToString());

    }

    finally

    {

    if (Response != null)

    {

    Response.Close();

    }

    }

    return (image);

    }

     

     

    private void Form1_Load(object sender, EventArgs e)

    {

    RetrieveImage(new Uri("http://glasssteelandstone.com/image.jpg"));

    }

    }

    }



     


  • WebRequest is breaking me in 2.0