我需要在Microsoft Visual C# 2008 Express Edition中使用System.Net,但似乎无法使任何网络类型的控件或类起作用。下面的WebClient示例总是抛出异常“无法连接到远程服务器”,因此我也无法加载WebBrowser控件的页面。
以下是代码(已编辑):
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
using (WebClient client = new WebClient()) {
string s = client.DownloadString("http://www.google.com");
this.textBox1.Text = s;
}
}
}
}
这是一个简单的表单,里面只有一个文本框控件(多行设置为true)。异常在DownloadString(...)
行抛出。我还尝试使用WebRequest
,但是出现了同样的异常!
编辑:
我连接到Linksys WRT54G路由器,该路由器直接连接到我的电缆调制解调器。我没有在代理服务器后面,虽然我运行了proxycfg -u
,结果如下:
Updated proxy settings
Current WinHTTP proxy settings under:
HKEY_LOCAL_MACHINE\
SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections\
WinHttpSettings :
Direct access (no proxy server).
我正在使用Windows XP,没有运行任何防火墙。目前只有AVG。我相信我不需要转发任何端口或其他东西,但我尝试在我的路由器上将端口80转发到我的工作站上。但并没有帮助。
proxycfg -u
也许是值得的;另外,您是否运行了任何激进的防火墙?请检查是否有任何阻止/拒绝条目... - Marc Gravell