本文介绍了WebClient.DownloadingString要求改变的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我把网址在浏览器,我的服务器响应正常(一个XML)。尽管如此,如果此相同的URL通过WebClient.DownloadingString()方法中,一些在URL发生变化,而我的服务器响应正常,但拒绝访问消息(XML,太),好像有什么东西发生了变化。

错误消息

&LT; XML版本=1.0编码=ISO-8859-1&GT;&LT;说:服务xmlns:said="http:xxx"><said:codigo_erro>8</said:codigo_erro><said:mensagem_erro>Unable</said:mensagem_erro></said:service>

在请求中使用的URL是像这样的:

<$p$p><$c$c>http://...<parameter1>S<%2Fparameter1>%0D%0A++<parameter2>S<%2Fparameter2>%0D%0A++<parameter3>S<%2Fparameter3>%0D%0A<%2Fqueryservice>%0D%0A%09%09

我已经尝试改变去恩code到UT8,ISO等,他们中的任何人工作。

解决方案

您必须确保你将所有必要的数据,cookies和服务器期望请求头。

我劝你安装提琴手网页调试和监控Web浏览器成功的请求,之后尝试重新创建你的应用程序这样的请求。

也许服务器重定向你一些错误页面,因为 Web客户端不处理的cookies。你可以创建自己的版本 Web客户端并添加cookie支持。创建inhertis从 Web客户端和覆盖 GetWebRequest 方法的类,但你必须添加的CookieContainer 。下面是一个简单的执行 Web客户端处理的cookie:

 公共类MyWebClient:Web客户端
{
    公众的CookieContainer的CookieContainer {获得;私定; }

    公共MyWebClient()
    {
        this.CookieContainer =新的CookieContainer();
    }

    保护覆盖的WebRequest GetWebRequest(URI地址)
    {
        WebRequest的请求= base.GetWebRequest(地址);
        如果(请求的HttpWebRequest)
        {
            (要求为HttpWebRequest的).CookieContainer = this.CookieContainer;
            (要求为HttpWebRequest的).AllowAutoRedirect = TRUE;
        }

        返回请求;
    }
}
 

If I put the URL at the browser, my server responds properly (a XML).Although, if this same URL pass through the WebClient.DownloadingString() method, something in the URL changes, and my server responds properly, but with an access denied message (XML, too), as if something had changed.

"Error message"

<?xml version="1.0" encoding="ISO-8859-1"?><said:service xmlns:said="http:xxx"><said:codigo_erro>8</said:codigo_erro><said:mensagem_erro>Unable</said:mensagem_erro></said:service>

The URL used on request is like this one:

http://...<parameter1>S<%2Fparameter1>%0D%0A++<parameter2>S<%2Fparameter2>%0D%0A++<parameter3>S<%2Fparameter3>%0D%0A<%2Fqueryservice>%0D%0A%09%09

I have already tried change de Encode to UT8, ISO, etc. No one of them worked.

解决方案

You have to be sure that you're sending all the necessary data, cookies and request headers that the server is expecting.

I advise you to install Fiddler Web Debugger and monitor successful requests from web browser, after that try to recreate such requests in your application.

Maybe server is redirecting you to some error page because WebClient is not handling cookies. You can create your own version of WebClient and add cookie support. Create a class that inhertis from WebClient and override GetWebRequest method, there you have to add CookieContainer. Following is a simple implementation of WebClient that handles cookies:

public class MyWebClient : WebClient
{
    public CookieContainer CookieContainer { get; private set; }

    public MyWebClient()
    {
        this.CookieContainer = new CookieContainer();
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = this.CookieContainer;
            (request as HttpWebRequest).AllowAutoRedirect = true;
        }

        return request;
    }
}

这篇关于WebClient.DownloadingString要求改变的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 00:39