【发布时间】:2019-07-05 05:44:39
【问题描述】:
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)
{
//
}
事实上,如果我们在 fiddler 中观察到,Post 调用是成功的,并且响应代码是 201-created。但是 post 调用返回的 Json 响应导致错误,因此 PostAsync 无法准备 HttpResponseMessage 对象。
我没有看到粘贴在下面的响应 JSON 有任何问题,这可能导致 - 找到无效字符。
同时提供原始请求和响应。
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"}}
【问题讨论】:
-
为什么响应正文是缩进的?那里可能有非打印的 UTF 字符吗?要解决问题,请尝试自己阅读正文并在单独的步骤中反序列化 JSON。
-
@John - 现在添加了 C# 代码。它在这条线上失败 responseMessage = await client.SendAsync(request);
-
@JohnWu - 它在 httpClient.SendAsync 本身失败。如果我得到 HttpResponseMessage 对象,我可以尝试使用自定义/动态对象进行解析。
-
@NareshPodishetty 为什么你认为
SendAsync失败了?响应中没有错误,您发布的代码实际上 隐藏SendAsync的异常,并且您发布的 sn-p 通常由 UWP 应用程序引发。发布实际异常,而不仅仅是其中的一部分 -
我赞成这个问题,因为这是一个奇怪的例外。
标签: c# api odata httpclient