HttpClient.SendAsync或PostAsync抛出以下错误。
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Exception: The text associated with this error code could not be found.
'': Invalid characters found.
at System.Net.Http.HttpHandlerToFilter.SendAsync(HttpRequestMessage request, CancellationToken cancel)
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at TestApp.MainPage.PostRequest(String url, StringContent stringContent)
这是C#代码:
var address = new AddressModel()
{
CommunicationId = "email@domain.com",
......
};
var stringContent = new StringContent(JsonConvert.SerializeObject(address), Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
client.DefaultRequestHeaders.Add("Cookie", AuthCookie);
client.DefaultRequestHeaders.Add("X-Requested-With", "X");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = stringContent//CONTENT-TYPE header
};
try
{
//Failing on this Line.
responseMessage = await client.SendAsync(request);
if (responseMessage.IsSuccessStatusCode)
{
var responseContent = await responseMessage.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
//
}
实际上,如果我们在提琴手中观察到,则Post调用成功,并且响应代码是201创建的。但是post调用返回的Json响应导致错误,因此PostAsync无法准备HttpResponseMessage对象。
我看不到下面粘贴的响应JSON可能引起的任何问题-找到无效字符。
同时提供Raw请求和响应。
POST http://host/sap/opu/odata/sap/EMPLOYEE_ADDRESS_SRV/ADDRESS_ODATA HTTP/1.1
Cookie: MYSAPSSO2=---------;
X-Requested-With: X
Accept: application/json
Content-Length: 619
Content-Type: application/json; charset=utf-8
Host: host:8080
Connection: Keep-Alive
Pragma: no-cache
{"start_date":"20090704","emailid":"email@domain.com","pernr":"123456","subty":"2","begda":"\/Date(1246838400000)\/","endda":"\/Date(1561950000000)\/","stext":"Home","name2":"Xyz","stras":"Address line one","locat":"Address line two","anssa":"1","grpvl":"1","land1":"IN","hsnmr":"2-3","landx50":"India","state":"10","bezei":"Karnataka","ort02":"fsd","ort01":"Bengaluru","entkm":"0","pstlz":"560097","telnr":"1234567890","persg":null,"persk":null,"bukrs":"safs","ename":"afjdsklfsl","persa":null,"molga":null,"country":null,"name1":"asfsd","comp_code":"ABC","emp_curr":"INR","message":"Sample"}
响应:
HTTP/1.1 201 Created
set-cookie: sap-usercontext=sap-client=300; path=/
set-cookie: SAP_SESSIONID_DHR_100=asjkfhskdhfks%3d; path=/
content-type: application/json; charset=utf-8
content-length: 1117
location: http://host:8080/sap/opu/odata/sap/EMPLOYEE_ADDRESS_SRV/ADDRESS_ODATA(emailid='email@domain.com',pernr='123456',subty='2',seqnr='000',start_date='20090704')
dataserviceversion: 2.0
message type: S
custom message: Home has been created successfully
sap-processing-info: ODataBEP=,crp=,st=,MedCacheHub=SHM,codeployed=X,softstate=
sap-perf-fesrec: 232519.000000
{"d":{"__metadata":{"id":"http://host:8080/sap/opu/odata/sap/EMPLOYEE_ADDRESS_SRV/ADDRESS_ODATA(emailid='email@domain.com',pernr='123456',subty='2',seqnr='000',start_date='20090704')","uri":"http://host:8080/sap/opu/odata/sap/EMPLOYEE_ADDRESS_SRV/ADDRESS_ODATA(emailid='email@domain.com',pernr='123456',subty='2',seqnr='000',start_date='20090704')","type":"EMPLOYEE_ADDRESS_SRV.ADDRESS_ODATAType"},"emailid":"email@domain.com","pernr":"123456","subty":"2","seqnr":"000","start_date":"20090704","begda":"\/Date(1246838400000)\/","endda":"\/Date(1561939200000)\/","stext":"Home","stras":"Address line one","locat":"Address line two","anssa":"1","grpvl":"1","name2":"Xyz","hsnmr":"2-3","ort01":"Bengaluru","ort02":"fsd","pstlz":"560097","land1":"IN","landx50":"India","telnr":"1234567890","entkm":"0","state":"10","bezei":"Karnataka","persg":"","persk":"","bukrs":"safs","ename":"afjdsklfsl","persa":"","molga":"","country":"","name1":"asfsd","comp_code":"WST","emp_curr":"INR","message":"Sample"}}
答案 0 :(得分:1)
感谢大家的回应和提出建议。
问题出在响应头属性上
message type: S
custom message: Home has been created successfully
键的内部有空格。在将它们更改为消息类型和自定义消息并用hiphen代替空格后-效果很好。