WP7 WebBrowser control headers
HI, is possible to add request headers in WP7 WebB开发者_开发知识库rowser control?
There is no way to do this. If you need to change headers you'll need to use HttpWebRequest
.
You could intercept the requests from the WebBrowser control and make them yourself via HWR but this could get complicated very quickly.
No - I don't think there's any API hook available for this.
It's a similar problem to the "change the user agent" request discussed in Bring back mobile version of website in WebBrowser control for wp7?
Sorry to necro but the answers here are wrong. Headers can be added to a WebBrowser through the Navigate method.
WebBrowser.Navigate(YourURI, null, YourCustomHeaderString)
See this page: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff626636(v=vs.105).aspx
.
These headers will only apply to the first page navigated to through your code. If you want the headers to stay the same even when users click a link inside the web browser control, add this for the WebBrowser's navigating event:
private void browser_Navigating(object sender, NavigatingEventArgs e)
{
string url = e.Uri.ToString();
if(!url.Contains("YESHEADERS"))
{
e.Cancel = true;
string newUrl;
if(url.Contains("?"))
{
newUrl = url + "&YESHEADERS";
}
else
{
newUrl = url + "?YESHEADERS";
}
browser.Navigate(newUrl, null, "fore:" + Variables.GetForeground() + "")
}
}
Here's what that does:
We create an indicator, YESHEADERS
, that tells us whether or not we have added custom headers.
When the WebBrowser tries to Navigate, we check whether or not the URL it is navigating to, e.Uri, contains YESHEADERS
.
If it does, we've already added our headers. Take no action
If it does not, cancel the current navigation. Create a new URL equal to the old URL plus our indicator. We add YESHEADERS
on to the new URL in it's query string. If you are not familiar with query strings that is fine, just know that they are extra strings on the URL that have no effect in our case. About Query Strings
Then, we navigate to the new URL, and add our custom headers.
In short, if we have our indicator YESHEADERS
the web browser knows that we've added our custom headers, if we don't have YESHEADERS
, than the web browser needs to add the headers.
精彩评论