likewer
likewer

Reputation: 216

WebBrowser navigate via session cookie in window phone

I am trying to login a website by POST method through HttpWebRequest. In the beginning, i create a portrait page to let user to enter username and password and access the web page. I successfully to Post the data and get the HTML content from the website. Finally I using the Webrowser.NavigateTostring method to display the website in the webBrowser I create.

Problem 1: Inside the webBrowser, I can not do any action in the website. In the normal webBrowser, after successfully login I can do any action such as go to event forum..but what i face is I can't navigate to event forum or any forum.

Problem 2: Inside the webBrowser, the image will not showing eg, user picture, product picture. I also trying to use IsolatedStorage method to show the HTML content but it also non work.

What I confusing is wheather the Webrowser.NavigateTostring method is a way to asynchronous operation to the website in internet? and what I doing wrong? or I looking for wrong direction?

Any help would be appreciated. Thank you.

Upvotes: 1

Views: 2108

Answers (2)

Chee Chen
Chee Chen

Reputation: 37

Unfortunately, Set-Cookie is one of the HTTP headers that are not supported.

Upvotes: 2

Paul Annetts
Paul Annetts

Reputation: 9604

This way will suffer from these problems as if you load the HTML using WebBrowser.NavigateToString or from Isolated Storage none of the relative links to images, scripts or CSS will work. Also it is impossible to pass any Cookies from HttpWebRequest to the WebBrowser

The way to do this is to use the WebBrowser control itself to do the POST. There is a overload of the WebBrowser.Navigate method as documented on MSDN here. That will allow you to POST data to your URL.

// generate your form data based on the data you got from your "portrait page" 
// and get the bytes from that.
// (e.g. write your post data to a MemoryStream as UTF8 and get its bytes)
byte[] formBytes = ...

// write HTTP headers here, including the type of data you're posting, e.g.:
string headers = "Content-Type: application/x-www-form-urlencoded" 
   + Environment.NewLine;


Uri uri = ... // where you want the POST data to be sent

this.webBrowser.Navigate(uri, formBytes, headers);

That way your web browser will be properly initialized and your cookies, images, scripts and CSS should all work.

Upvotes: 1

Related Questions