Xamarin中的HTTP请求

Xamarin中的HTTP请求,xamarin,httprequest,Xamarin,Httprequest,我在Xamarin中编写了以下代码来连接Web服务器: var request = WebRequest.Create( "http://srv21.n-software.de/authentication.json") as HttpWebRequest; // request.Method = "GET"; request.Method = "POST"; request.Headers.Add("name", "demo");

我在Xamarin中编写了以下代码来连接Web服务器:

        var request = WebRequest.Create( "http://srv21.n-software.de/authentication.json") as HttpWebRequest;
        // request.Method = "GET";
        request.Method = "POST";
        request.Headers.Add("name", "demo");
        request.Headers.Add("password", "demo");
        request.ContentType = "application/x-www-form-urlencoded";
        HttpWebResponse Httpresponse = (HttpWebResponse)request.GetResponse();
它连接到web服务器,web服务器获取“authentication.json”请求,但不获取头的参数(“name”和“password”)。
我的代码有什么问题?

您的参数很可能需要在
POST
请求的主体中,而不是在标题中。或者,如果您的服务器支持,您可以尝试使用
GET
请求,并通过URL提供参数(即
http://srv21.n-software.de/authentication.json?name=demo&password=demo
)。

这对我很有效

using System.Net.Http;

string URL = "http://www.here.com/api/postForm.php";
string DIRECT_POST_CONTENT_TYPE = "application/x-www-form-urlencoded";

HttpClient client = new HttpClient();
string postData = "username=usernameValueHere&password=passwordValueHere");

StringContent content = new StringContent(postData, Encoding.UTF8, DIRECT_POST_CONTENT_TYPE);
HttpResponseMessage response = await client.PostAsync(DIRECT_GATEWAY_URL, content);

string result = await response.Content.ReadAsStringAsync();

您是如何在服务器上读取这些标题的?您应该始终尝试解释您的答案,并说明其工作原理。我仍在自学。所以我无法解释。web服务器似乎没有在标头中查找值。也请原谅没有使用。那个HttpClient正在请求一个。