简体   繁体   English

如何使用承载令牌发布JSon

[英]How to POST JSon with a Bearer Token

I am able to do a GET with similar code and the Bearer Token, but cannot seem to do the POST. 我可以使用类似的代码和Bearer令牌进行GET,但似乎无法执行POST。

When I copy/paste the Json, the URL and the Bearer Token into Postman it works perfectly. 当我将Json,URL和Bearer Token复制/粘贴到Postman时,它可以正常工作。 But when doing it from the C# I get this error: 但是从C#执行此操作时,出现此错误:

"StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Transfer-Encoding: chunked Connection: keep-alive Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" CF-RAY: 4c190ad8ad786539-SYD Date: Wed, 03 Apr 2019 06:38:54 GMT Set-Cookie: __cfduid=dc0232e99fa0fefc0bd728258229dd5d51554273534; expires=Thu, 02-Apr-20 06:38:54 GMT; path=/; domain=.paymentsapi.io; HttpOnly; Secure Server: cloudflare X-Powered-By: ASP.NET Content-Type: application/json; charset=utf-8 }" “ StatusCode:400,ReasonPhrase:“错误请求”,版本:1.1,内容:System.Net.Http.StreamContent,标头:{Transfer-Encoding:chunked Connection:keep-alive Expect-CT:max-age = 604800,报告-uri =“ https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct” CF-RAY:4c190ad8ad786539-SYD日期:2019年4月3日,星期三06:38:54 GMT Set-Cookie: __cfduid = dc0232e99fa0fefc0bd728258229dd5d51554273534; expires = Thu,02-Apr-20 06:38:54 GMT; path = /; domain = .paymentsapi.io; HttpOnly; Secure Server:cloudflare X-Powered-By:ASP.NET Content-Type: application / json; charset = utf-8}“

Can anyone see what I'm doing wrong? 谁能看到我在做什么错?

Thanks in advance. 提前致谢。

JsonSerializerSettings jss = new JsonSerializerSettings();
string strValue = JsonConvert.SerializeObject(TestMaster, jss);
lblJSon.Text = strValue;        // This Json is valid
ByteArrayContent bytecontent = new ByteArrayContent(System.Text.Encoding.UTF8.GetBytes(strValue));
bytecontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

string AccessToken = lblToken.Text;

HttpClient tRequest = new HttpClient();
tRequest.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

Task<HttpResponseMessage> getTask = tRequest.PostAsJsonAsync(new Uri(strURL).ToString(), bytecontent);

HttpResponseMessage urlContents = await getTask;

Console.WriteLine("urlContents.ToString");
lblEDDR.Text = urlContents.ToString();

PostAsJsonAsync converts your ByteArrayContent into a json object. PostAsJsonAsync将您的ByteArrayContent转换为json对象。 You can use ether PostAsJsonAsync directly with your TestMaster like so: 您可以使用醚PostAsJsonAsync直接与您的TestMaster像这样:

string AccessToken = lblToken.Text;

HttpClient tRequest = new HttpClient();
tRequest.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

Task<HttpResponseMessage> getTask = tRequest.PostAsJsonAsync(new Uri(strURL).ToString(), TestMaster);

HttpResponseMessage urlContents = await getTask;

Console.WriteLine("urlContents.ToString");
lblEDDR.Text = urlContents.ToString();

Or you convert TestMaster to Json and use PostAsync with a StringContent object. 或者你转换TestMaster以JSON和使用PostAsyncStringContent对象。 Like so: 像这样:

JsonSerializerSettings jss = new JsonSerializerSettings();
string strValue = JsonConvert.SerializeObject(TestMaster, jss);
lblJSon.Text = strValue;        // This Json is valid
StringContent strcontent = new StringContent (strValue);
bytecontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

string AccessToken = lblToken.Text;

HttpClient tRequest = new HttpClient();
tRequest.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

Task<HttpResponseMessage> getTask = tRequest.PostAsync(new Uri(strURL).ToString(), bytecontent);

HttpResponseMessage urlContents = await getTask;

Console.WriteLine("urlContents.ToString");
lblEDDR.Text = urlContents.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM