使用c#和httpclien向Rest Api发布请求

2024-05-17 17:53:31 发布

您现在位置:Python中文网/ 问答频道 /正文

有这样的代码:

var client = new HttpClient();
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("username:", " 1"));
postData.Add(new KeyValuePair<string, string>("password:", " 1"));

HttpContent content = new FormUrlEncodedContent(postData);

HttpResponseMessage response = await client.PostAsync(
    "http://libdiary.liberty-lab.ru/api/v1/login", 
    content);

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

他必须向服务器发出post请求。这个项目是windows phone。 服务器上负责此查询的功能如下(Python):

^{pr2}$

但是,尝试访问windows phone时总是使用error:INTERNAL SERVER ERROR,并且在应用程序代码中返回带有error on this page的http页面。你能告诉我是什么问题吗?也许我发送数据是错误的,你能建议如何做的正确吗?在


Tags: 服务器clientaddhttpnewstringresponsevar
2条回答

您试图访问名为HTTP_USERNAMEHTTP_USERNAME的键上的request对象,这些键不存在。在呼叫站点或目标站点更改它们:

username = request.META['username']
password = request.META['password']

我解决了问题。服务器检查邮件头中的用户名和密码。我只添加了一个新的标题“username”和新的头“password”,并为它们分配适当的值。在

  HttpContent content = new FormUrlEncodedContent(postData);
        content.Headers.Add("username", "1");
        content.Headers.Add("password", "2");

谢谢你的帮助,尤其是尤瓦尔·伊扎科夫。在

相关问题 更多 >