1

我正在尝试在 WinRT 应用程序中发送下面的 POST 请求。

http://i.stack.imgur.com/I18Bh.png

这是我使用的代码:

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
}

它可以工作,因为我从服务器获取 HTML 代码,但我收到的页面不包含查询结果,它只是没有结果的搜索页面。

似乎遗漏了请求中的某些内容,有什么建议吗?

4

1 回答 1

0

您缺少将该对数组转换为百分比编码的字符串。不幸的是,WinRT 中没有NameValueCollection类。但是做一个等价的功能并不难。例如:

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);
}

然后,只需从代码中调用函数并将结果传递给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.
    }
}

仅此而已,其余代码按预期工作。

更新:

您的某些键是错误的,它是DesLocDest而不是DescLocDest

您肯定需要设置 cookie,至少以ASPSESSIONId...开头的那个。

如果这还不够,请尝试设置User-AgentOrigin标头。

于 2013-11-14T07:02:07.977 回答