WebClient.UploadData (posting, getting session ID and posting again)

S

Sonnich Jensen

Hi all

This is for an internal system.

A C# app, which needs to login onto a web interface (this works) then read out some reports.
However getting some session ID from the login is new to me, but I need that to continue to open the search page.

Just how do I get that and how do I pass it on?

On the internet I can find some ideas of how to pass it on, but I need get it first

WBR
Sonnich
 
A

Arne Vajhøj

This is for an internal system.

A C# app, which needs to login onto a web interface (this works) then read out some reports.
However getting some session ID from the login is new to me, but I need that to continue to open the search page.

Just how do I get that and how do I pass it on?

On the internet I can find some ideas of how to pass it on, but I need get it first

With form based authentication and simple setups then the login returns
a session cookie and you just need to send that with the following
requests.

That is simple with (Http)WebRequest, but requires a little trick with
WebClient.

public class WebClientWithCookies : WebClient
{
private CookieContainer cookies = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest res = base.GetWebRequest(address);
if(res != null && res is HttpWebRequest)
{
((HttpWebRequest)res).CookieContainer = cookies;
}
return res;
}
}


Arne
 
A

Arne Vajhøj

With form based authentication and simple setups then the login returns
a session cookie and you just need to send that with the following
requests.

Note that ASP.NET web forms with view state can be more tricky.

Arne
 
S

Sonnich Jensen

Note that ASP.NET web forms with view state can be more tricky.

Arne

I can post to the loginpage, and I found out about the __viewstate - I needto post that, then - it somewhat works - but I can get an error for a failed login, but no further action when providing the right password.

I looked into what the viewstate holds of data, and I cannot see that I should change that we reporting it back.

When I try to open the next page, I get a "500 internal server error", which does not tell me that much.... then again it is a part of a frameset (menu + page). I have no idea what that ca do to it. The link from the menu will simply open a page, and I can open it in a new window. Still not from myprogramme

WBR
Sonnich
 
A

Arne Vajhøj

I can post to the loginpage, and I found out about the __viewstate - I need to post that, then - it somewhat works - but I can get an error for a failed login, but no further action when providing the right password.

I looked into what the viewstate holds of data, and I cannot see that I should change that we reporting it back.

When I try to open the next page, I get a "500 internal server error", which does not tell me that much.... then again it is a part of a frameset (menu + page). I have no idea what that ca do to it. The link from the menu will simply open a page, and I can open it in a new window. Still not from my programme

As I said then it can be very tricky.

I got it to work once by re-POST'ing both __VIEWSTATE and
__EVENTVALIDATION.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top