简体   繁体   English

C#中System.Net的Webclient无法连接到服务器

[英]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 ,不是httpcfgproxycfg -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.

相关问题 C#-System.Net Socket.Listen积压 - C# - System.Net Socket.Listen Backlog 初学者问题-Visual C#-System.Net汇编和WebRequest - Beginners Question - Visual C# - System.Net Assembly and WebRequest Fedora 25中的Dotnet:System.Net中不存在WebClient - Dotnet in Fedora 25: WebClient does not exist in System.Net 为什么WebClient(System.Net)从URL获得两次? - Why is WebClient (System.Net) getting from URL twice? C# HttpClient 请求无法抓取(在 System.Net 和 Windows.Web http 请求上) - C# HttpClient request fails to scrape (both on System.Net and Windows.Web http requests) C#阅读 <system.net><mailSettings> 在外部DLL的web.config中 - C# Read <system.net><mailSettings> in web.config from external dll 在 C# 中以编程方式从 app.config 访问 system.net 设置 - Access system.net settings from app.config programmatically in C# 在c#的“ System.Net.NetworkCredential”中使用未过载的webclient输入网页身份验证 - enter webpage authentication with webclient that's not overloaded in “System.Net.NetworkCredential” in c# C#.net System.Net.WebClient ..服务器端还是没有? - C#.net System.Net.WebClient .. Server Side or not? 不返回1/5 cookie,C#简短而可爱的代码:RichTextBox,Form1_Load,使用System.Net / System.Collections.Specialized - Not returning 1/5 cookies, C# short and sweet code: RichTextBox, Form1_Load, using System.Net / System.Collections.Specialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM