gmanzoli
gmanzoli

Reputation: 77

WinRT HttpClient, POST request

I'm trying to send the POST request below in a WinRT App.

https://i.sstatic.net/I18Bh.png

This is the code i use:

var pairs = new List<KeyValuePair<string, string>>
{
  new KeyValuePair<string, string>("MinOraPart", "01:00"),
  new KeyValuePair<string, string>("MaxOraPart", "23:59"),
  new KeyValuePair<string, string>("TIPOVIS", "FERMATE"),
  new KeyValuePair<string, string>("CAMBIOCOMUNE", "0"),
  new KeyValuePair<string, string>("DescLocPart", "PADOVA AUTOSTAZIONE"),
  new KeyValuePair<string, string>("DescLocDest", "ROVIGO AUTOSTAZIONE"),
  new KeyValuePair<string, string>("direzione", "ANDATA"),
  new KeyValuePair<string, string>("gg", ""),
  new KeyValuePair<string, string>("meseanno", ""),
  new KeyValuePair<string, string>("ControlloEsisteFermata", "0"),
  new KeyValuePair<string, string>("PARTENZA", ""),
  new KeyValuePair<string, string>("LocPartenza", "348|PADOVA AUTOSTAZIONE|0"),
  new KeyValuePair<string, string>("ARRIVO", ""),
  new KeyValuePair<string, string>("LocArrivo", "453|ROVIGO AUTOSTAZIONE|0"),
  new KeyValuePair<string, string>("dataViaggio", "14/11/2013"),
  new KeyValuePair<string, string>("OREDalSol", "01:00"),
  new KeyValuePair<string, string>("OREAlSol", "23:59"),
  new KeyValuePair<string, string>("fascia", "libera"),
  new KeyValuePair<string, string>("ordine", "NumCambi, OraPart"),
  new KeyValuePair<string, string>("MaxNodi", "1"),
  new KeyValuePair<string, string>("MinimoV", "0"),
  new KeyValuePair<string, string>("CERCA_ANDATA", "corse di ANDATA")
}
var content = new StringContent(pairs);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var client = new HttpClient();
var response = await client.PostAsync("http://ro.autobus.it/ro/asp/RicercaOrari.asp?User=SITA", content);
if (response.IsSuccessStatusCode)
{
    //Extract the data from the webpage
}

It works since i get the HTML code from the server, but the page i recive doesn't contain the query result, it's just the search page without the results.

It seems that miss something in the request, any suggestion?

Upvotes: 1

Views: 2818

Answers (1)

kiewic
kiewic

Reputation: 16440

You are missing to convert that array of pairs into a percent-encoded string. Unfortunately, there is no NameValueCollection class in WinRT. But it is not too hard to make an equivalent function. E.g.:

private string ToPercentEncoding(List<KeyValuePair<string, string>> pairs)
{
    List<string> joinedPairs = new List<string>();
    foreach (var pair in pairs)
    {
        joinedPairs.Add(
            System.Net.WebUtility.UrlEncode(pair.Key) +
            "=" +
            System.Net.WebUtility.UrlEncode(pair.Value));
    }

    return String.Join("&", joinedPairs);
}

Then, just call the function from your code and pass the result to the StringContent class:

private async void Foo(){
    var pairs = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("MinOraPart", "01:00"),
        new KeyValuePair<string, string>("MaxOraPart", "23:59"),
        new KeyValuePair<string, string>("TIPOVIS", "FERMATE"),
        new KeyValuePair<string, string>("CAMBIOCOMUNE", "0"),
        new KeyValuePair<string, string>("DescLocPart", "PADOVA AUTOSTAZIONE"),
        new KeyValuePair<string, string>("DescLocDest", "ROVIGO AUTOSTAZIONE"),
        new KeyValuePair<string, string>("direzione", "ANDATA"),
        new KeyValuePair<string, string>("gg", ""),
        new KeyValuePair<string, string>("meseanno", ""),
        new KeyValuePair<string, string>("ControlloEsisteFermata", "0"),
        new KeyValuePair<string, string>("PARTENZA", ""),
        new KeyValuePair<string, string>("LocPartenza", "348|PADOVA AUTOSTAZIONE|0"),
        new KeyValuePair<string, string>("ARRIVO", ""),
        new KeyValuePair<string, string>("LocArrivo", "453|ROVIGO AUTOSTAZIONE|0"),
        new KeyValuePair<string, string>("dataViaggio", "14/11/2013"),
        new KeyValuePair<string, string>("OREDalSol", "01:00"),
        new KeyValuePair<string, string>("OREAlSol", "23:59"),
        new KeyValuePair<string, string>("fascia", "libera"),
        new KeyValuePair<string, string>("ordine", "NumCambi, OraPart"),
        new KeyValuePair<string, string>("MaxNodi", "1"),
        new KeyValuePair<string, string>("MinimoV", "0"),
        new KeyValuePair<string, string>("CERCA_ANDATA", "corse di ANDATA")
    };

    var content = new StringContent(ToPercentEncoding(pairs));
    content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
    var client = new HttpClient();
    var response = await client.PostAsync("http://localhost", content);
    if (response.IsSuccessStatusCode)
    {
        //Extract the data from the webpage.
    }
}

And that's all, the rest of your code works as expected.

UPDATE:

Some of your keys are wrong, it is DesLocDest and not DescLocDest.

You will definitely need to set the cookie, at least the one starting with ASPSESSIONId....

If that's not enough, try setting the User-Agent and Origin headers.

Upvotes: 0

Related Questions