Asked by:
web api exception : Error while copying content to a stream

Question
-
User173155391 posted
Http Client throwing error : Error while copying content to a stream
This is my code
serverUrl = GetServerUri(serverUrl);
HttpContent content = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
client.BaseAddress = new Uri(serverUrl);
content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsync(url, content).Result;
return response.Content.ReadAsStringAsync().Result;It works fine when i deploy web api in root level. like : http:\\servername\api\controller\method
But when i deploy web api like this : http:\\servername\apiname\api\controller\method
This throws this exception.
Any idea
Monday, April 9, 2018 10:57 AM
All replies
-
User-369506445 posted
hi
you must define a custom Routing in your config , please add below Route in your WebApiConfig below like :
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // this is a new route config.Routes.MapHttpRoute( name: "DefaultApi_new", routeTemplate: "{apiname}/api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }
now you can try both of urls
Monday, April 9, 2018 11:14 AM -
User283571144 posted
Hi sarathchandrajith,
Do you mean you send the request to this url " http:\\servername\apiname\api\controller\method" not working?
Does the application show 404 error or some other error status?
Have you enable the custom route for your web api?
If you could post more details information, it will be more easily for us to find the solution.
Best Regards,
Brando
Friday, April 13, 2018 6:23 AM -
User173155391 posted
Hi Brando,
This is the error message :
Error while copying content to a stream
No i didn't enabled the custom route. But this is the only method throw this error.. All other methods are working as expected from angularjs. This call is from c# and a post call with a content throws this error
Thanks
Sarath
Sunday, April 15, 2018 4:23 AM -
User-369506445 posted
hi
please refer to below links:
Sunday, April 15, 2018 4:49 AM -
User173155391 posted
Hi,
HttpContent content = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
string serverNameOne= "http://servername/wespapi/api/task/GetGanttTasks";
string ServerName= "http://servername/api/task/GetGanttTasks";
var response = client.PostAsync(durl1,content).Result;
return response.Content.ReadAsStringAsync().Result;This is my client code to call web api. All other methods are working fine with serverNameOne address. only here i have a problem. when i use ServerName without the sub root it works fine.
Exception details :
- InnerException {"Error while copying content to a stream."} System.Exception {System.Net.Http.HttpRequestException}Inner Exception
- InnerException {"The request was aborted: The connection was closed unexpectedly."} System.Exception {System.Net.WebException}Any luck ?
Thanks
Sarath
Monday, June 11, 2018 11:36 AM