Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# net中的代理_C# - Fatal编程技术网

C# net中的代理

C# net中的代理,c#,C#,我尝试使用以下代码访问web进行xml解析: System.Uri proxy = new System.Uri("http://usr:pwd@10.1.121.2:8080"); System.Net.WebRequest.DefaultWebProxy = new System.Net.WebProxy(proxy); string url = "http://url/"; XmlTextReader reader = new

我尝试使用以下代码访问web进行xml解析:

        System.Uri proxy = new System.Uri("http://usr:pwd@10.1.121.2:8080");
        System.Net.WebRequest.DefaultWebProxy = new System.Net.WebProxy(proxy);
        string url = "http://url/";
        XmlTextReader reader = new XmlTextReader(url);
        richTextBox1.Text = Convert.ToString(reader.Read());
但它给出了一个代理身份验证所需的错误。我已经设置了代理参数。那么,有人能提出必要的改变以使其发挥作用吗

        System.Uri proxy = new System.Uri("http://usr:pwd@10.1.121.2:8080");
        System.Net.WebRequest.DefaultWebProxy = new System.Net.WebProxy(proxy);
        string url = "http://url/";
        XmlTextReader reader = new XmlTextReader(url);
        richTextBox1.Text = Convert.ToString(reader.Read());
WebProxy proxy = new WebProxy("172.0.0.1:8080", true);    
proxy.Credentials = new NetworkCredential("user", "passw", "domain");
然后,使用WebRequest通过代理检索数据流

        System.Uri proxy = new System.Uri("http://usr:pwd@10.1.121.2:8080");
        System.Net.WebRequest.DefaultWebProxy = new System.Net.WebProxy(proxy);
        string url = "http://url/";
        XmlTextReader reader = new XmlTextReader(url);
        richTextBox1.Text = Convert.ToString(reader.Read());
WebRequest dstream = WebRequest.Create("http://data-stream-url.com/file.ext");
dstream.Proxy = proxy;

您必须在
WebRequest.DefaultWebProxy
对象上设置
Credentials
属性
CredentialCache.DefaultNetworkCredentials
可能可以完成这项工作。

我对C#知道的不多,但是System.Uri真的那么聪明吗?它能自动解析并理解第一个字符串中的用户和密码吗?我用python这样说。我是C#的新手。我教过它会工作…即使在给出了它的引发异常“远程服务器返回错误:(407)需要代理身份验证”之后,你是否可能没有正确地使用代理服务器进行身份验证?代理现在正在工作,但我需要知道如何从web请求对象获取xml?请查看以下链接,并结合我上面的回答:
        System.Uri proxy = new System.Uri("http://usr:pwd@10.1.121.2:8080");
        System.Net.WebRequest.DefaultWebProxy = new System.Net.WebProxy(proxy);
        string url = "http://url/";
        XmlTextReader reader = new XmlTextReader(url);
        richTextBox1.Text = Convert.ToString(reader.Read());