使用xamarin表单连接到服务器

时间:2014-11-26 07:06:46

标签: xamarin cross-platform

我正在开发Xamarin Cross平台应用程序。我正在尝试连接到服务器(http://test.net/login/clientlogin),我需要发送这些字段(password =“xyz”; platform = iphone;(useremail)=“test@test.com”;)以及请求。这样服务器就会检查这些参数并返回XML。但我们不知道如何将这些字段添加到请求中。

当我打开上面的字符串url(http:// ***** / login / clientlogin)时,我正在登录屏幕,我们有用户名,密码和平台文本字段。

提前致谢!!! ..

1 个答案:

答案 0 :(得分:0)

这应该让您开始假设您在请求中将值添加为标题:

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");