C# 中的 System.Net Web 客户端无法连接到服务器

发布于 2024-07-11 18:55:28 字数 1282 浏览 10 评论 0原文

我需要做些什么才能让 System.Net 与 Microsoft Visual C# 2008 Express Edition 一起使用吗? 我似乎根本无法让任何 Web 类型控件或类工作。下面的 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 转发到路由器上的工作站。 没有帮助。

Is there something I need to do to get System.Net working with 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.

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. The exception gets thrown on the DownloadString(...) line. I also tried using WebRequest.. same exception!

EDIT:

I am connected to a Linksys WRT54G Router that connects directly to my cable modem. I am not behind a proxy server, although I did run proxycfg -u and I got:

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. Only AVG at the moment. 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. Didn't help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花间憩 2024-07-18 18:55:28

更新 - 我的意思是 proxycfg,而不是 httpcfgproxycfg -u 将执行导入)

首先,有这里的“express”没什么特别的。 其次,contoso 是一个虚拟 URL。

你使用什么操作系统? 您是否通过代理服务器? 如果是这样,您可能需要配置操作系统的 http 堆栈 - proxycfg 将在 XP 上完成这项工作,并且可用于导入用户的 IE 设置。

该示例很好,尽管它没有正确处理多个 IDisposable 对象 - 以下要简单得多:

        using (WebClient client = new WebClient()) {
            string s = client.DownloadString("http://www.google.com");
            // do something with s
        }

(update - I meant proxycfg, not httpcfg; proxycfg -u will do the import)

First, there is nothing special about "express" here. Second, contoso is a dummy 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.

The sample is fine, although it doesn't correctly handle the multiple IDisposable objects - the following is much simpler:

        using (WebClient client = new WebClient()) {
            string s = client.DownloadString("http://www.google.com");
            // do something with s
        }
故事未完 2024-07-18 18:55:28

您的 PC 上是否有可能影响它的防火墙软件? 您是否尝试过 Google 以外的其他网站?

Do you have any firewall software on your PC that might be affecting it? Have you tried with any sites other than Google?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文