在 WebClient wp7 中重定向时获取当前 Uri
我希望我不会开始一个已经完成的话题,但我在这里或其他地方都没有找到任何正确的答案。 那么我们开始吧: 我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HttpWebRequest
是这里的解决方案,因为WebClient
无论如何都是它的包装器。像这样的事情应该适合您的具体情况:我在主页构造函数中启动请求,然后在回调中处理它。请注意我如何获取
ResponseUri
- 您的最终目的地。如果您不想阻止重定向并只是获取 URL(就像我在上面的代码片段中所做的那样),则无需处理
AllowAutoRedirect
。HttpWebRequest
is the solution here, sinceWebClient
is a wrapper around it anyway. Something like this should work for your specific situation: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.