简体   繁体   English

WinRT HttpClient,POST请求

[英]WinRT HttpClient, POST request

I'm trying to send the POST request below in a WinRT App. 我正在尝试在WinRT应用中发送下面的POST请求。

http://i.stack.imgur.com/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. 因为我从服务器获取HTML代码,所以它可以工作,但是我接收的页面不包含查询结果,它只是没有结果的搜索页面。

It seems that miss something in the request, any suggestion? 似乎错过了请求中的某些内容,有什么建议吗?

You are missing to convert that array of pairs into a percent-encoded string . 您缺少将对对数组转换为百分比编码的string的想法 Unfortunately, there is no NameValueCollection class in WinRT. 不幸的是,WinRT中没有NameValueCollection类。 But it is not too hard to make an equivalent function. 但是,要实现同等功能并不是很困难。 Eg: 例如:

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: 然后,只需从代码中调用该函数,然后将结果传递给StringContent类:

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 . 您的某些键是错误的,它是DesLocDest而不是DescLocDest

You will definitely need to set the cookie, at least the one starting with ASPSESSIONId... . 您肯定需要设置cookie,至少要设置一个以ASPSESSIONId ...开头的cookie。

If that's not enough, try setting the User-Agent and Origin headers. 如果这还不够,请尝试设置User-AgentOrigin标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM