大家好,我正在尝试通过使用 php Web 服务登录来从 url 下载数据,但我不知道该怎么做。如果我通过邮递员发出 POST 请求,在 BODY -> form-data 中插入电子邮件和密码,它会给出以下内容:
{“用户”:[{“电子邮件”:“email@test.it”,“昵称”:“昵称”,“图像”:“http://localhost/MyWebService/images/test_img.png “}]}
所以我在cs中创建了一个类,如下所示:
public class Users
{
[JsonProperty("users", NullValueHandling = NullValueHandling.Ignore)]
public User[] UsersUsers { get; set; }
}
public class User
{
[JsonProperty("email", NullValueHandling = NullValueHandling.Ignore)]
public string email { get; set; }
[JsonProperty("nickname", NullValueHandling = NullValueHandling.Ignore)]
public string nickname { get; set; }
[JsonProperty("password", NullValueHandling = NullValueHandling.Ignore)]
public string password { get; set; }
[JsonProperty("image", NullValueHandling = NullValueHandling.Ignore)]
public Uri image { get; set; }
}
相反,在 botton 函数中,我尝试编写此代码来联系服务并进行调用:
async private void login(Object sender, EventArgs e)
{
string email = lbl_email.Text;
string password = lbl_password.Text;
string url = "http://192.168.178.77/TestLoginURL/api/login.php";
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("email", email),
new KeyValuePair<string, string>("password", password),
});
string contentType = "application/json";
JObject json = new JObject
{
{ "email", email},
{ "password", password }
};
HttpClient client = new HttpClient();
var response = await client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, contentType));
var data = await response.Content.ReadAsStringAsync();
var users = JsonConvert.DeserializeObject<Users>(data);
}
显然它被压垮了,给我的错误是这样的:Newtonsoft.Json.JsonReaderException 解析值时遇到意外字符:<。路径 '',第 0 行,位置 0。
那么,有人可以帮助我吗?我哪里错了?谢谢
冉冉说
相关分类