WinRT HttpClient,POST请求

时间:2013-11-13 22:23:33

标签: c# .net windows-runtime dotnet-httpclient

我正在尝试在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代码,但我重新接收的页面不包含查询结果,它只是没有结果的搜索页面。

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

1 个答案:

答案 0 :(得分:0)

您缺少将该对数组转换为percent-encoded string。不幸的是,WinRT中没有NameValueCollection类。但是,制作同等功能并不难。 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);
}

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

如果还不够,请尝试设置 User-Agent Origin 标题。