在 WebClient wp7 中重定向时获取当前 Uri

发布于 2024-11-27 11:01:46 字数 802 浏览 2 评论 0原文

我希望我不会开始一个已经完成的话题,但我在这里或其他地方都没有找到任何正确的答案。 那么我们开始吧: 我使用 WebClient 从网页下载 HTML 代码,然后使用该 WebClient 发送新请求,并且网页会重定向我。现在我想知道网站把我放在哪里了。

WebClient 类本身没有任何合适的属性,我已经尝试重写该类,以便我可以获得响应 URI,但不知何故它不适用于 wp7。

那么有什么想法如何获取我的 WebClient 重定向的 URI 吗?或者知道为什么当我想使用自己的类时应用程序崩溃:

    public class MyWebClient : WebClient
    {
            Uri _responseUri;

            public Uri ResponseUri
            {
                get { return _responseUri; }
            }
            protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
            {
                WebResponse response = base.GetWebResponse(request, result);
                _responseUri = response.ResponseUri;
                return response;
            }
        }
}

提前致谢!

I hope that I won't start a topic that's done already but I didn't find any proper answer here nor anywere else.
So here we go:
I use a WebClient to download HTML Code from a webpage, then I send a new request with that WebClient and the WebPage redirects me. Now I want to now where the Site has put me.

The WebClient Class itself doesn't have any suitable properties, I already tried to rewrite the class so that I could get the Response URI but somehow it doesn't work for wp7.

So any ideas how to get the URI where my WebClient got redirected? Or any idea why the application crashes when I want to use my own class:

    public class MyWebClient : WebClient
    {
            Uri _responseUri;

            public Uri ResponseUri
            {
                get { return _responseUri; }
            }
            protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
            {
                WebResponse response = base.GetWebResponse(request, result);
                _responseUri = response.ResponseUri;
                return response;
            }
        }
}

Thanks in advance!

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

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

发布评论

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

评论(1

冷夜 2024-12-04 11:01:47

HttpWebRequest 是这里的解决方案,因为 WebClient 无论如何都是它的包装器。像这样的事情应该适合您的具体情况:

private HttpWebRequest request;
private bool flagIt = true;

public MainPage()
{
    InitializeComponent();

    request = (HttpWebRequest)WebRequest.Create("http://google.com");
    request.BeginGetResponse(new AsyncCallback(GetData), request);
}

public void GetData(IAsyncResult result)
{
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);

    Debug.WriteLine(response.ResponseUri.ToString());

    if (flagIt)
    {
        request = (HttpWebRequest)WebRequest.Create("http://microsoft.com");
        request.BeginGetResponse(new AsyncCallback(GetData), request);
        flagIt = false;
    }
}

我在主页构造函数中启动请求,然后在回调中处理它。请注意我如何获取 ResponseUri - 您的最终目的地。

如果您不想阻止重定向并只是获取 URL(就像我在上面的代码片段中所做的那样),则无需处理 AllowAutoRedirect

HttpWebRequest is the solution here, since WebClient is a wrapper around it anyway. Something like this should work for your specific situation:

private HttpWebRequest request;
private bool flagIt = true;

public MainPage()
{
    InitializeComponent();

    request = (HttpWebRequest)WebRequest.Create("http://google.com");
    request.BeginGetResponse(new AsyncCallback(GetData), request);
}

public void GetData(IAsyncResult result)
{
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);

    Debug.WriteLine(response.ResponseUri.ToString());

    if (flagIt)
    {
        request = (HttpWebRequest)WebRequest.Create("http://microsoft.com");
        request.BeginGetResponse(new AsyncCallback(GetData), request);
        flagIt = false;
    }
}

I am initiating the request in the main page constructor and then I am handling it in the callback. Notice how I am getting the ResponseUri - your final destination.

You don't need to handle AllowAutoRedirect if you don't want blocking the redirect and simply getting the URL, like I am doing in the snippet above.

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