C# WinRT HttpClient,POST请求

C# WinRT HttpClient,POST请求,c#,.net,windows-runtime,dotnet-httpclient,C#,.net,Windows Runtime,Dotnet Httpclient,我正在尝试在WinRT应用程序中发送下面的POST请求 这是我使用的代码: var pairs = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("MinOraPart", "01:00"), new KeyValuePair<string, string>("MaxOraPart", "23:59"), new KeyValuePai

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

这是我使用的代码:

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代码,但是我收到的页面不包含查询结果,它只是没有结果的搜索页面


请求中似乎遗漏了什么,有什么建议吗?

您缺少将成对数组转换为一个数组的功能。不幸的是,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);
}
仅此而已,其余的代码按预期工作

更新:

你的一些钥匙错了,它是DesLocDest而不是DesLocDest

您肯定需要设置cookie,至少是以ASPSESSIONId开头的cookie


如果这还不够,请尝试设置用户代理和源标题。

是否有可与之比较的工作查询?快速看一眼,它看起来还可以…截图中的查询是有效的,我没有该页面的源代码。这是完整的postdata字符串:MinOraPart=16%3A30&MaxOraPart=23%3A59&TIPOVIS=FERMATE&CAMBIOCOMUNE=0&DesLocPart=PADOVA+AUTOSTAZIONE&DesLocDest=ROVIGO+AUTOSTAZIONE&direzione=ANDATA&gg=&meseanno=&Controlleosistefermata=0&PARTENZA=PADOVA+AUTOSTAZIONE&LocPart=348%7CPADOVA+AUTOSTAZIONE%7C0&ARRIVO=ROVIGO+AUTOSTAZIONE&direzione=453%7CROVIGO+Ta2FE&Loczione=2013%20%DalSel=16%3A30&OREAlSel=23%3A59&筋膜=libera&ordine=NumCambi%2C+OraPart&MaxNodi=2&MinimoV=0&CERCA_ANDATA=corse+di+ANDATASee,如果您可以使用Fiddler捕捉工作/非工作状态。它可能是一个cookie、HTTP标头等。当您没有可用的服务器源代码时,很难调试此类问题。我第一次通过浏览器访问页面时收到一个ASP会话的cookie。您是否在代码中发送它?如果你想简化你的应用程序,你应该提出同样的要求,然后去掉多余的东西。仍然不能工作。。。我甚至尝试使用var content=newstringcontentpostdatastring;其中postDataString是我从Tamper数据中获得的编码字符串,Firefox的一个插件,显示get/PostRequestWhat到底不起作用?您可以共享请求和响应的Fiddler跟踪吗?仍然不够,修复了密钥,添加了会话cookie和用户代理/源标题。现在请求是:POST/ro/asp/RicercaOrari.asp?User=SITA HTTP/1.1 Accept:text/plain User Agent:Mozilla/5.0 Referer:http://ro.autobus.it/ro/asp/RicercaOrari.asp?User=SITA 来源:http://ro.autobus.it 接受语言:它;q=0.8内容类型:应用程序/x-www-form-urlencoded主机:ro.autobus.it Cookie:ASPSESSIONIDCQBCDBAD=biimchccmjmghelmampagof内容长度:470预期:100继续
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.
    }
}