could not create ssl/tls secure channel when call an API - c#

i'm get this code from a curl-to-c# convertor site
curl is working but in this code i get error: could not create ssl/tls secure channel
using System.Net.Http.Headers;
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://www.alsatpardakht.com/IPGAPI/Api22/send.php");
request.Headers.Add("Cookie", "PHPSESSID=td3qg4c25oo5jrft5m0sd48om1");
request.Content = new StringContent("Amount=20000&ApiKey=YOUR APIKEY&Tashim=%5B%5D&RedirectAddressPage=www.test.com&PayId=111");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
if anyone to try real, i can give him the api-key to test it
this api call works in other lang but not in c#

Related

Is it possible to generate headers automatically with HttpClient / RestSharp?

At the moment I am using the following RestSharp request to get a website's content:
var client = new RestClient(productLink);
var request = new RestRequest(Method.GET);
request.AddHeader("Cookie", "insert-cookie-content");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
I have tried converting it into HttpClient as i will need to use the AllowRedirect property later:
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Cookie", "insert-cookie-content");
var response = await client.GetAsync(productUrl);
Console.WriteLine(response);
The URL I am trying to get a response from is: https://www.nike.com/sg/launch/t/air-max-90-orange-duck-camo
My first problem is that the HttpClient request is giving me 403 Errors whereas the RestClient request was working fine. How can I fix this?
My second problem is that the cookie expires after a couple of uses, and I have to manually get a new one from postman and insert it. Is there anyway for the request to generate its own cookie?
Here is the two fiddler responses compared: https://imgur.com/a/bZo7d9F
In case of HttpClient if you want to pass the Cookies manually through the DefaultRequestHeaders then you have to tell this to the HttpClient to do NOT use CookieContainer. You have to use HttpClientHandler's UseCookie flag to indicate it.
var client = new HttpClient(new HttpClientHandler { UseCookies = false });
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Cookie", "insert-cookie-content");
var response = await client.GetAsync(productUrl);
Console.WriteLine(response);

SOAP Request with HttpClient

I'am trying to reach a SOAP API using the HttpClient object. I've searched everywhere but most of the people are using the HttpWebRequest object which is not supported by the DNX Core framework.
Does anyone have a working example of a SOAP request using the HttpClient object?
This image represents a simple request from this API (NuSOAP PHP):
Thank you!
EDIT :
So I was able to call the API with the following code:
Uri uri = new Uri("http://localhost/teek_api/service.php");
HttpClient hc = new HttpClient();
hc.DefaultRequestHeaders.Add("SOAPAction", "http://localhost/teek_api/service.php/ping");
var content = new StringContent("text/xml; charset=utf-8");
using (HttpResponseMessage response = await hc.PostAsync(uri, content))
{
var soapResponse = await response.Content.ReadAsStringAsync();
string value = await response.Content.ReadAsStringAsync();
return value;
}

HttpClient with chunk data not working

When I execute the query in Firefox/IE I get the full response; but when I execute the same request with HttpClient I get only a part. I don't understand why.
Apparently the data are chunked that's why I specify the ResponseContentRead.
var requestUri = "https://api.guildwars2.com/v2/continents/1/floors/1";
HttpClient httpClient = new HttpClient();
var request = new HttpRequestMessage();
request.Method = HttpMethod.Get;
request.RequestUri = requestUri;
var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);
var result = await response.Content.ReadAsStringAsync();
Why Firefox/IE returns the right response and HttpClient an incomplete one ?
Result from HttpClient:
Result from Firefox/IE:
The code is inside a C# UWP app.
I have successfully ran your code and gotten the entire json string. However, I had to make a minor modification in order to get it working:
var requestUri = new Uri("https://api.guildwars2.com/v2/continents/1/floors/1");
HttpClient httpClient = new HttpClient();
var request = new HttpRequestMessage();
request.Method = HttpMethod.Get;
request.RequestUri = requestUri;
var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);
var result = await response.Content.ReadAsStringAsync();
Note the requestUri being initialized as an Uri instance instead of a string instance.
Also, the code has been tested in a console application.
EDIT: Here is a paste of what I got from the call. I've beautified the code to make it more readable. Perhaps it helps you validate if the content is as expected: Json result

Performing a put or patch request with token

I've got a chunk of code:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", "Username", "password"))));
var method = new HttpMethod("PATCH");
var reqmsg = new HttpRequestMessage(method, uri)
{
Content = new StringContent(request, Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await client.SendAsync(reqmsg);
This works fine using Basic authentication. I want to use a token though, and if I change the Authorization to use a webtoken:
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", WebToken);
I now get a 403 Forbidden error. If I'm doing a Post or a Get, the token works, but not for Patch or Put. I'm guessing the token is somehow being stripped off. is there a way around this?
You're getting a 403 error because the encoding is incorrect.
-facepalm-
You're using UTF8 while the default is ANCI... Doesn't make sense.

Adding authentication header to HttpClient

I'm trying to access an API, but all the documentation is in PHP and I'm not very familiar with PHP. I am having trouble authenticating to the API. The documentation is here.
Here is what I have so far
var webAddress = "https://xboxapi.com/v2/latest-xbox360-games";
var httpResponse = (new HttpClient().GetAsync(webAddress)).Result;
httpResponse.EnsureSuccessStatusCode();
var jsonResponse = httpResponse.Content.ReadAsStringAsync().Result;
I'm just not sure how to add the authentication header that they are using in PHP.
Any help would be appreciated.
To add a custom header (in this case X-AUTH), you need to send a custom HttpRequestMessage. For example:
var webAddress = "https://xboxapi.com/v2/latest-xbox360-games";
HttpClient client = new HttpClient();
HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, webAddress);
msg.Headers.Add('X-AUTH', 'your-auth-key-here');
HttpResponseMessage response = await client.SendAsync(msg);

Categories