在 WebClient wp7 中重定向时获取当前 Uri
我希望我不会开始一个已经完成的话题,但我在这里也没有找到任何正确的答案.所以我们开始:我使用 WebClient 从网页下载 HTML 代码,然后使用该 WebClient 发送新请求,WebPage 重定向我.现在我想要现在站点放置我的位置.
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.
WebClient 类本身没有任何合适的属性,我已经尝试重写该类以便获得响应 URI,但不知何故它不适用于 wp7.
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.
那么任何想法如何获取我的 WebClient 被重定向的 URI?或者知道为什么当我想使用我自己的类时应用程序崩溃:
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;
}
}
}
提前致谢!
HttpWebRequest
是这里的解决方案,因为 WebClient
无论如何都是它的包装器.这样的事情应该适用于您的特定情况:
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;
}
}
我在主页面构造函数中发起请求,然后在回调中处理它.请注意我如何获得 ResponseUri
- 您的最终目的地.
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.
如果您不想阻止重定向并简单地获取 URL,则不需要处理 AllowAutoRedirect
,就像我在上面的代码段中所做的那样.
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.