C#Web客户端和代理服务器 [英] C# webclient and proxy server

查看:112
本文介绍了C#Web客户端和代理服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Web客户端类在我的源代码code使用HTTP下载的字符串。

这是工作的罚款。然而,在该公司的客户都现在连接到代理服务器。而从这个开始的问题。

当我测试了我的申请,我不认为它可以通过代理服务器,作为不断得到抛出的异常是不从xxx.xxx.xxx.xxx答复是代理服务器的IP地址。

不过,我仍然可以浏览到该网站的网址,并通过代理服务器连接时正确显示字符串中的浏览器,而不是当我用我的网络客户端。

有什么在Web客户端,我必须配置,让我从后面代理服务器访问URL?

 使用(Web客户端WC =新的WebClient())
{
    字符串strURL =HTTP:// xxxxxxxxxxxxxxxxxxxxxxxx    //下载只有当Web客户端不旺。
    如果(!wc.IsBusy)
    {
        字符串rtn_msg =的String.Empty;
        尝试
        {
            rtn_msg = wc.DownloadString(新的URI(strURL));
            返回rtn_msg;
        }
        赶上(引发WebException前)
        {
            Console.Write(ex.Message);
            返回false;
        }
        赶上(异常前)
        {
            Console.Write(ex.Message);
            返回false;
        }
    }
    其他
    {
        System.Windows.Forms.MessageBox.Show(忙,请重试);
        返回false;
    }
}


解决方案

我的解决办法:

  Web客户端的客户端=新的WebClient();
WebProxy WP =新WebProxy(代理服务器地址在这里);
client.Proxy =可湿性粉剂;
字符串str = client.DownloadString(http://www.google.com);

I am using a web client class in my source code for downloading a string using http.

This was working fine. However, the clients in the company are all connected now to a proxy server. And the problem started from this.

When I have tested my application I don't think it can pass through the proxy server, as the exception that keeps getting thrown is "no response from xxx.xxx.xxx.xxx which is the proxy server IP address.

However, I can still navigate to the web site URL and it displays the string correctly in the browser when connecting through a proxy server, but not when I use my web client.

Is there something in the web client that I have to configure to allow me to access the url from behind a proxy server?

using (WebClient wc = new WebClient())
{
    string strURL = "http://xxxxxxxxxxxxxxxxxxxxxxxx";

    //Download only when the webclient is not busy.
    if (!wc.IsBusy)
    {
        string rtn_msg = string.Empty;
        try
        {
            rtn_msg = wc.DownloadString(new Uri(strURL));
            return rtn_msg;
        }
        catch (WebException ex)
        {
            Console.Write(ex.Message);
            return false;
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
            return false;
        }
    }
    else
    {
        System.Windows.Forms.MessageBox.Show("Busy please try again");
        return false;
    }
}

解决方案

My solution:

WebClient client = new WebClient();
WebProxy wp = new WebProxy(" proxy server url here");
client.Proxy = wp;
string str = client.DownloadString("http://www.google.com");

这篇关于C#Web客户端和代理服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆