[英]System.Net's Webclient in C# won't connect to server
Is there something I need to do to get System.Net working with Microsoft Visual C# 2008 Express Edition? 为了使System.Net与Microsoft Visual C#2008 Express Edition一起使用,我需要做些什么? I can't seem to get any web type controls or classes to work at all.. the below WebClient example always throws the exception " Unable to connect to the remote server ".. and consequently I can't get the WebBrowser control to load a page either.
我似乎根本无法获得任何Web类型的控件或类来工作。.下面的WebClient示例始终抛出异常“ 无法连接到远程服务器 ”。因此,我无法加载WebBrowser控件一页。
Here is the code (Edited): 这是代码(已编辑):
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;
}
}
}
}
This is in a simple form with only a textbox control (with multiline set to true) in it. 这是一种简单形式,其中仅包含一个文本框控件(多行设置为true)。 The exception gets thrown on the
DownloadString(...)
line. 在
DownloadString(...)
行上引发异常。 I also tried using WebRequest
.. same exception! 我也尝试使用
WebRequest
..同样的异常!
EDIT: 编辑:
I am connected to a Linksys WRT54G Router that connects directly to my cable modem. 我已连接到直接连接到电缆调制解调器的Linksys WRT54G路由器。 I am not behind a proxy server, although I did run
proxycfg -u
and I got: 我没有运行代理服务器,尽管我确实运行
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).
I am using Windows XP and not running any kind of firewall. 我正在使用Windows XP,但没有运行任何类型的防火墙。 Only AVG at the moment.
目前只有AVG。 I'm pretty sure I shouldn't have to forward any ports or anything, but I did try forwarding port 80 to my workstation on my router.
我很确定我不必转发任何端口或任何东西,但是我确实尝试过将端口80转发到路由器上的工作站。 Didn't help.
没有帮助。
( update - I meant proxycfg
, not httpcfg
; proxycfg -u
will do the import) ( 更新 -我的意思是
proxycfg
,不是httpcfg
; proxycfg -u
将执行导入)
First, there is nothing special about "express" here. 首先,这里的“表达”没有什么特别的。 Second, contoso is a dummy url.
其次,contoso是伪URL。
What OS are you on? 您在什么操作系统上? And do you go through a proxy server?
而且您要通过代理服务器吗? If so, you might need to configure the OS's http stack -
proxycfg
will do the job on XP, and can be used to import the user's IE settings. 如果是这样,则可能需要配置操作系统的http堆栈
proxycfg
将在XP上完成此工作,并可用于导入用户的IE设置。
The sample is fine, although it doesn't correctly handle the multiple IDisposable
objects - the following is much simpler: 该示例很好,尽管它不能正确处理多个
IDisposable
对象-以下更为简单:
using (WebClient client = new WebClient()) {
string s = client.DownloadString("http://www.google.com");
// do something with s
}
Do you have any firewall software on your PC that might be affecting it? 您的PC上是否有任何可能会影响它的防火墙软件? Have you tried with any sites other than Google?
您是否尝试过Google以外的任何其他网站?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.