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

问题描述 投票:0回答:1

我使用 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();

    }
}

}

visual-studio-2010 httpwebrequest
1个回答
5
投票

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

request.Proxy = null;

我不知道为什么会这样,但愿上帝保佑。

© www.soinside.com 2019 - 2024. All rights reserved.