Got error when access https URL using HttpClient - c#

i'm using Visual Studio to build Android application. I'm using REST API to communicate the app with the server, and later i got this error
System.Net.Http.HttpRequestException: An error occurred while sending the request
this is my codes
var client = new HttpClient();
client.DefaultRequestHeaders.Add(defaultData.headerName, defaultData.headerData);
var json = await client.GetStringAsync("https://example.com/api");
var items = JsonConvert.DeserializeObject<ArdilesmetroClass>(json);
This problem occurs when i try to accessing https url and it works fine when accessing http. What i need to add in my codes?

Done, i have the answer.Go to your Project Properties->Android Options-> Click Advanced. You need to change SSL/TLS implementation to
Native TLS 1.2+

Related

CloudFare error 1020 Access Denied in C# .net core

Hi I am trying to access an image hosting website's Api, I am able to upload files to it but if I try to retrieve the file suing the Api then an error "CloudFare error 1020 Access Denied " kind of is shown to me if I try to access it using any Tool like Postman or RestClient in C#, but when I retrieve the file using the download button on the website then I am not shown any error and Image is downloaded properly.
Here is my RestClient Code:-
var client = new RestClient("https://anonymousfiles.io/f/camps_5.jpg");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
JObject json = JObject.Parse(response.Content.ToString());
I had a similar issue using curl and cloudFare. Add a user agent header with "Chrome/79" and there is a good chance that you will not get CloudFare error 1020 Access Denied

Using GET request causes bot dialog to fail

I've connected my bot application to the direct line API which is published with Azure. I am currently testing the application with a command line client application, the bot framework emulator, and the dev.botframework.com homepage for my bot.
Everything works correctly until I attempt to submit a GET request to a REST API. I've tested the GET API request in a separate project and it works correctly and the GET request worked prior to implementing the direct line channel. Is there anything I need to be aware of when making http requests with the direct line on the bot side?
Code in question
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", headerParam);
var response = client.GetAsync(new Uri("someUrl.com/api/v1/auth")).Result;
string content = response.Content.ReadAsStringAsync().Result;
var jo = JObject.Parse(content);
this.token = jo["Result"]["Token"].ToString();
}
await context.PostAsync(this.token);
the line that actually causes the failure is
var response = client.GetAsync(new Uri("someUrl.com/api/v1/auth")).Result;
Also is there an easier way to debug a project when it's published to azure and running direct line API?
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions ipaddress
I have tried to invoke my custom REST API within my bot application back-end, then I could leverage Remote debugging web apps to retrieve the result from my bot application hosted on Azure web app as follows:
After searching for the related issue, I found that there be limitation for the number of sockets of your current app service plan when creating the new outgoing connections. You could try to scale up your App Service plan or create a new web app to isolate this issue. For more details, you could refer to this similar issue and this blog.

401 when authenticating with clickatel sms api

I am simply trying to send an sms using the clickatel API.
I have used the helper library found here: https://github.com/clickatell/clickatell-csharp
var apiClient = new RestClient(new RESTCredentials(ApiKey));
var result = apiClient.Authenticate();
Debug.Write("_apiClient.Authenticate() = " + result.Result);
and I get the following:
"Error occured during Clickatell Authenticate. Details: The remote server returned an error: (401) Unauthorized."
I have checked the api key several times, I have even regenerated a new on on their portal.
Am I missing something?
I see you're using the old Clickatell Csharp library that was written for the old endpoint.
You will need to use the new script which can be found at https://github.com/clickatell/Clickatell-Csharp-Platform
Hopefully this will resolve the issue.

How to properly establish SSL connection from UWP App to WebAPI using HttpClient class?

I have an UWP application which communicates with server WebAPI. To do this I am using HttpClient class from Windows.Web.Http namespace. It does work fine if I use simple Http URL. Because of sending passwords and other sesitive data I would like to use SSL. But the problem appears when I am trying to use Https URL. Program still shows me an error which says that "The certificate authority is invalid or incorrect". I am new in certificates etc. so it does not tell me anything useful.
On some websites I found some semi-solution. Using HttpBaseProtocolFilter class I can set options to ignore some server certificate errors like this:
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
HttpClient client = new HttpClient(filter);
It works, but only on desktop. When I run my application on Mobile Emulator, the error shows up again. Also I do not really think that this is a good solution so I have searched for something else and just found this: https://pfelix.wordpress.com/2012/12/16/using-httpclient-with-ssltls/ . There are some informations about HttpClient and SSL, but as far as I understand it is about HttpClient from namespace System.Net.Http. I tried this solution for Windows Store App, but it have not worked too.
var client = new HttpClient(new HttpClientHandler
{
ClientCertificateOptions = ClientCertificateOption.Automatic
});
So here is my question: how can I set up this HttpClient (from Windows.Web.Http or System.Net.Http) to perform working SSL connection with my WebAPI? Maybe is there something to set by server side?
Edit: I am using IIS Express from Visual Studio 2015 because I do not really know how to add my WebAPI to full IIS and let it work. I also turn on SSL in WebAPI project Properties, which generated for me a Https URL (so I thought that it would also generate a properly certificate). If I try to navigate to the Http URL everything is fine on my Mozilla Firefox and IE. The error about untrusted certificate shows when I try my https://localhost:44301/ URL in both browsers.
You were close with the HttpClientHandler approach, but you want to trust the server certificate, not the client certificate (which doesn't exist in your example). Try this:
HttpClient client = new HttpClient(new HttpClientHandler {
ServerCertificateCustomValidationCallback = (a, b, c, d) => true });
This will cause ALL server certificates to be trusted so don't use it in production, but works great for accessing an https service on localhost from a UWP app during development.

How can I get more information about a fault in system.net.http?

I'm using HttpClient from System.Net.Http on nuget to make a request from a module (assembly) written in c#. This needs to work with a legacy application in Windows XP so we're using .Net Framework 4.0.
I can successfully send a https request to our server from my Windows 10 machine; however, when I test it from Windows XP in a VM, I get a fault. I can request websites like https://google.com but our https site causes a fault. On top of all this, the res.StackTrace is null! The Message I get from obj.Exception.InnerException is An error ocurred while sending the request. I can however get to our site in a browser in the VM without trouble. So why can't I get a StackTrace? Could this be some kind of ssl trust issue (our site has an ssl certificate from godaddy)?
var client = new HttpClient();
client.BaseAddress = new Uri(SERVER_BASE_URL); // https://...
// Works when BaseAddress="https://google.com" but not our https site
return client.GetAsync("/").ContinueWith(res =>
{
if (res.IsFaulted)
// res.StackTrace is empty >:(
MessageBox.Show("Faulted");
else
MessageBox.Show("Success");
});

Categories