当前位置: 动力学知识库 > 问答 > 编程问答 >

json - HttpClient does not work in asp.net (works in Postman)

问题描述:

I tested this code and it worked first time; now I get empty response. However, with Postman I am able to get the response. It seems that Json is not read correctly. I get back a 400 error.

private Guid GetToken()

{

var client = new HttpClient();

var uri = "url";

var jsonInString = JsonConvert.SerializeObject(new Authorization());

var response = client.PutAsync(uri, new StringContent(jsonInString, Encoding.UTF8, "application/json")).Result;

var x = response.Content.ReadAsStringAsync().Result;

if(x=="")

return Guid.Empty;

return new Guid(response.Content.ReadAsStringAsync().Result);

}

public class Authorization

{

public string _x;

public Guid _y;

public Guid _z;

public long _a;

public long _b;

public Authorization()

{

x = 123; //dummy data

y = xx-xx-xx-xxxxx-xx-xxxx;

z = xx-xx-xx-xxxxx-xx-xxxx;

a = 123;

b = 123;

}

}

网友答案:

I managed to solve it. The problem was the name of the attributes in the Json object.

I used Resharper and tat renamed my variables in the Json object from Id to _id (note the underscore).

分享给朋友:
您可能感兴趣的文章:
随机阅读: