HttpClient.SendAsync or PostAsync is throwing below error.
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)
Here is the C# Code:
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)
{
//
}
Infact, the Post call was successful and response code was 201-created if we observe it in fiddler. but the Json response the post call is returning is causing the error and hence PostAsync is failing to prepare HttpResponseMessage object.
I don't see any issue with the response JSON pasted below which could cause - invalid characters found.
Providing both the Raw request and response.
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"}
Response:
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"}}
Thanks everyone for responding and providing suggestions.
The issue was with the response header properties
message type: S
custom message: Home has been created successfully
key's has space in them. after changing them to message-type and custom-message with hiphen instead of space - it worked fine.