我在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”)。 我的代码出了什么问题?
答案 0 :(得分:1)
您的参数很可能需要在POST
请求的正文中,而不是在标题中。或者,您可以尝试使用GET
请求,并通过网址提供参数(如果您的服务器支持)(即http://srv21.n-software.de/authentication.json?name=demo&password=demo
)。
答案 1 :(得分:0)
这对我有用
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();