无法使用非常简单的httpWebRequest类应用程序连接远程服务器

时间:2012-11-08 05:48:51

标签: visual-studio-2010 httpwebrequest connect

我使用visual2010用httpWebRequest类编写一个简单的应用程序。第一次运行该应用程序时,它可以工作,但在取得一些成功后,它会被警告所困扰 “无法连接远程服务器”。 我已经阅读了很多网,但没有太多的线索可以做,几乎说因为反病毒软或防火墙导致问题,但当我关闭两者时,它仍然无法正常工作。我也重新安装了visual2010,但问题仍然存在

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace new_httpWebRequest
{
class Program
{
    static void Main(string[] args)
    {
        string result ="";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://my-favor.net");
        // line code problem below:
        `HttpWebResponse response = (HttpWebResponse)request.GetResponse();`
        var sr = new StreamReader(response.GetResponseStream() ?? System.IO.Stream.Null, Encoding.UTF8);
        result = sr.ReadToEnd();
        sr.Close();
        Console.Write(result);
        Console.ReadLine();

    }
}

}

1 个答案:

答案 0 :(得分:3)

最后,我通过添加这一行来找到解决方案:

request.Proxy = null;

我不知道它为什么会起作用,只要上帝保佑。