HTTPClient to simulate form submission on ASPX - I

2019-08-01 03:10发布

I am trying to simulate a form submission on an ASPX.NET site.

The flow of the website when accessed in a browser is as follows:

1) In a browser the user visits http://mysite.com/ which is configured with Basic Authentication

2) Upon correct credentials, the user is shown a form with one input text box and a button (URL stays http://mysite.com/ but the form being served is Default.aspx)

3)User enters some text and presses submit...

4) The page reloads... URL is still http://mysite.com/... but there is a timer which triggers after 10 secs and downloads a file from http://mysite.com/Downloader

I am trying to simulate this flow in my program using HTTPClient.

1) Do a GET on http://mysite.com

2) Extract hidden form fields __EVENTVALIDATION and __VIEWSTATE

3) Create a POST request with above two and other form fields and POST it to http://mysite.com RESULTS in Invalid Viewstate exception.

How do I achieve this in HTTPClient?

2条回答
相关推荐>>
2楼-- · 2019-08-01 03:30

The usual way to do this is as follows: First, record the HTTP traffic using WireShark or Fiddler while you are using the website from the browser. Second, analyze the packet trace in detail, and collect every HTTP header and every HTTP payload from every GET and POST message sent by the browser. Third, try to send the same messages from your code. After sending an HTTP request, you will have to analyze the response of the server, and extract all pieces of data you need to insert into the next request. Don't forget to set the referer field, for example. Add each request to your code one by one, and record the traffic when you run the code. If you assemble your HTTP requests correctly, then your request packets should look like the requests of the browser.

查看更多
Animai°情兽
3楼-- · 2019-08-01 03:30

I'm in the same scenario, I have to create a POST request to an external ASPX page. I have captured the traffic using FIDDLER and tryed to simulare the call using online post request tool like https://www.codepunker.com

I have not been able to recreate the request...

In my opinion (and this require time) we have to:

  1. Create a basic webrequest to the source form
  2. Collect all the form elements with value
  3. Create a POST request submitting all the elements including VIEWSTATE

NOTE: may be that you need to use a webclient that accepts cookies, check:

Accept Cookies in WebClient?

Good luck

查看更多
登录 后发表回答