我正在开发Xamarin Cross平台应用程序。我正在尝试连接到服务器(http://test.net/login/clientlogin),我需要发送这些字段(密码 = "xyz"; 平台 = iphone;(用户电子邮件)="test@test.com";)以及请求。因此,服务器将检查这些参数并返回 XML。但是我们不知道如何将这些字段添加到请求中。
当我打开上面的字符串网址(http://*****/login/clientlogin)时,我看到登录屏幕,其中我们有用户名,密码和平台文本字段。
提前谢谢!!..
这应该会让你开始假设你在请求中将值添加为标头:
public class TestClient
{
HttpClient client;
public TestClient(){
this.client = new HttpClient ();
}
public void AddHeadersAndGet(){
client.DefaultRequestHeaders.Add ("username", "whatevertheusernameis");
this.GetAsync<WhateverObjectTypeYouAreReceiving> ("theurloftheservice");
}
public async Task<T> GetAsync<T>(string address){
HttpResponseMessage response = null;
response = await client.GetAsync (address);
if (response.IsSuccessStatusCode) {
try {
var responseString = await response.Content.ReadAsStringAsync ();
return new T (Serializer.DeserializeObject<T> (responseString),
response.StatusCode);
} catch (Exception ex) {
}
} else {
}
}
}
关键行是:
client.DefaultRequestHeaders.Add ("username", "whatevertheusernameis");