![](/img/trans.png)
[英]POST request via HttpClient.PostAsync with StorageFile inside body (WinRT)
[英]WinRT HttpClient, POST request
我正在尝试在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代码,所以它可以工作,但是我接收的页面不包含查询结果,它只是没有结果的搜索页面。
似乎错过了请求中的某些内容,有什么建议吗?
您缺少将对对数组转换为百分比编码的string的想法 。 不幸的是,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 ...开头的cookie。
如果这还不够,请尝试设置User-Agent和Origin标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.