我试图在Xamarin中使用HttpClient PCL向其他api发帖。我有以下代码,但我得到了404.请注意,我没有真正的IP地址,但是当我使用Hurl.It和它真正的连接时它很好,但这里只是404我错过了什么?
string url = "http://myaddresshere/services/rest/auth/login";
string result = String.Empty;
using (var client = new HttpClient()) {
var content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>(txtUsername.Text, "username"),
new KeyValuePair<string, string>(txtPassword.Text, "password")
});
using (var response = await client.PostAsync(url, content)) {
using (var responseContent = response.Content) {
result = await responseContent.ReadAsStringAsync();
}
}
}
答案 0 :(得分:2)
您应该切换代码:
new KeyValuePair<string, string>(txtUsername.Text, "username"),
new KeyValuePair<string, string>(txtPassword.Text, "password")
要:
new KeyValuePair<string, string>("username", txtUsername.Text),
new KeyValuePair<string, string>("password", txtPassword.Text)