I really have no idea about the issue here.
I have a DLL to send requests to an HTTPS server and get response with below C# code
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string result = await response.Content.ReadAsStringAsync();
The DLL is with a standard interface. If this is called from my C# application, I got an error when read string from the response, sayiny "input string was not in a correct format". But if the URL is HTTP (not HTTPS) or it is called from a standard commercial application, it works.
Any help here is highly appreciated.
Related
I am trying to use a Rest API from outside my company's domain. My problem is when I run the POST request within Postman or an Angular application it works fine. But when I send the same payload within a winforms c# application, I get Access Forbidden. You have browsed from an unrecognized IP address. The proxy therefore denied access to this site.
I thought the problem was with the payload itself, so I copied the same JSON string, sent it via postman and It worked. So I thought perhaps the issue is with the server itself, so I ran other GET requests to the same server and they worked. I tried sending an empty string to the POST request and it worked, tried an empty object and get FORBIDDEN REQUEST. The same code, I ran it from my personal computer and it worked.
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", sToken);
StringContent sRequestContent = new StringContent(requestPayload, Encoding.UTF8, "application/json");sRequestContent).Result;
var result = client.PostAsync(new Uri(sUrl), sRequestContent).Result;
return result.Content.ReadAsStringAsync().Result;
PS:
requestPayload is a JSON string.
I'm using the C# class HttpClient to make requests to a RESTful API. Are these requests secure by virtue of using an HTTPS URI? Or should I be doing more in the code to make sure the requests are secure?
Here is some sample code:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://www.example.com");
client.DefaultRequestHeaders.Add("custom-header-1", "value-1");
client.DefaultRequestHeaders.Add("custom-header-2", "value-2");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(type, credentials);
HttpResponseMessage response = client.PostAsync(URLParameters, content).Result;
return response.Content.ReadAsStringAsync().Result;
I've read some material online with conflicting ideas:
Some people say that the URL being HTTPS is good enough.
Others say you have to manually take care of certificates in the code.
I'm confused as to which school of though is correct.
I would like to know what is the proper way to send a get request to a URL with # in it, for example http://xxx/#/home in old angularjs that still has # in their route?
I am using the .net HttpClient class, and I tried
HttpResponseMessage response = await client.GetAsync("http://xxx/#/home");
and i am getting badrequest in response until i remove the # part.
Any ideas?
Thanks.
I have a web service in a RESTful web server (java) which consumes media of type APPLICATION_FORM_URLENCODED and produces media of type MULTIPART_FORM_DATA. Now I'm working on a REST client (C#) and trying to use this web service. I'm using RestSharp as the REST client. My code goes as follows:
RestRequest request = new RestRequest("getDataFileChunkIS", Method.POST);
request.AddParameter("sessionId", sessionId);
request.AddParameter("dataFileId", dataFileId);
request.AddParameter("offset", offset);
request.AddParameter("chunkSize", chunkSize);
request.AddParameter("checksumFlag", checksumFlag);
RestClient client = new RestClient(url);
RestResponse response = (RestResponse)client.Execute(request);
But in this response I'm getting HTTP Status 406 - Not Acceptable. It says "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers." Maybe I'm doing it in a wrong way. So my question is that how can I execute this request whose response will contain MULTIPART_FORM_DATA ?
1) how can I execute this request whose response will contain MULTIPART_FORM_DATA?
request.AddHeader("Accept", "multipart/form-data")
2) how can I read this response header(contains JSON) using RestClient?
See answers to this question. Particularly the third one, which shows how to do it just with .NET 4.5 libraries.
You may need to implement IDeserializer to get access to the raw HttpResponse for consumption.
I have application built with C# that consume Restful built with Java Expecting XML as request body.How do i go about it ?
Need more more information on what you are doing specifically what c# http stack you are using. It should not matter what the technology the service is written.
Here is one way you could accomplish this
1) Install HttpClient from Nuget. It's version 0.5.0.0
2) Use the following code
var client = new System.Net.Http.HttpClient();
var url = #"http://localhost:9999/books";
var content = new StringContent("<book><title>some title</title></book>", Encoding.UTF8, "application/xml");
client.Post(url, content);
HttpClient is very easy to use and can be discussed here http://wcf.codeplex.com