你好,Stackoverflow社区。我希望这里有人能帮我!!
我试图与Zoopla集成,该API要求post请求发送以下自定义的内容类型。(我把证书方面的事情做得很好)
application/json;profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.2/schemas/listing/list.json
我尝试了以下方法,但没有成功(它们都会导致以下错误)
System.FormatException: 'The format of value 'application/json;profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.2/schemas/listing/list.json' is invalid.'
var HttpRequestMessage=新的RequestUri (){RequestUri=新Uri(“https://realtime-listings-api.webservices.zpg.co.uk/sandbox/v1/listing/list"”,Method = HttpMethod.Post,Content = new (jsonBody,Encoding.UTF8,"application/json;profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.2/schemas/listing/list.json") };
client
来自ClientFactory
)client.DefaultRequestHeaders.Add("Content-Type","application/json;profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.2/schemas/listing/list.json");
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type","application/json;profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.2/schemas/listing/list.json");
,
string header = "application/json;profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.2/schemas/listing/list.json";client.DefaultRequestHeaders.Accept.Add(新的client.DefaultRequestHeaders.Accept.Add)
我真是不知所措!!救命:-)
发布于 2022-10-11 18:27:21
Content-Type
设置在内容上,而不是在DefaultRequestHeaders
中。您可以尝试对请求内容使用TryAddWithoutValidation
:
var content = new StringContent("hello");
content.Headers.ContentType = null; // zero out default content type
content.Headers.TryAddWithoutValidation("Content-Type", "application/json;profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.2/schemas/listing/list.json");
var client = new HttpClient(); // note: use IHttpClientFactory in non-example code
var response = await client.PostAsync("https://postman-echo.com/post", content);
Console.WriteLine(response.StatusCode); // OK
Console.WriteLine(await response.Content.ReadAsStringAsync());
// {"args":{},"data":{},"files":{},"form":{},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-6345b568-22cc353761f361483f2c3157","content-length":"5","content-type":"application/json;profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.2/schemas/listing/list.json"},"json":null,"url":"https://postman-echo.com/post"}
https://stackoverflow.com/questions/71285584
复制相似问题