How to connect HTTPS in Win8 App with proxy - http

I can connect by http but can not connect https.
HttpRequestException Message:
An error occurred while sending the request.
System.Net.WebExceptionStatus.TrustFailure
HttpClientHandler clienthandler = new HttpClientHandler();
HttpClient client = new HttpClient(clienthandler);
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "https://www.google.com.hk/");
req.Headers.Referrer = new Uri("https://www.google.com.hk/");
HttpResponseMessage mes = await client.SendAsync(req);

Related

How to add Client Certificates ASP.NET 4.6

I'm trying to add this certificates into my code to make a HTTP Request on Asp.NET 4.6.
Anyone have any idea how to insert them on the request?
For now all I have in code is:
string privateKey = File.ReadAllText(#"C:\izio\clientid.key");
var certificate = new X509Certificate2(#"C:\izio\clientidwin.crt", #"");
HttpClientHandler handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ClientCertificates.Add(certificate);
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
HttpClient client = new HttpClient(handler);
Thanks!!

How to make a HTTP get call with request header in ASP.NET MVC to an API with Oauth2 authorization?

I'm having a hard time executing a http get call with headers to an api with oauth2 authorization.
I already tried the code below but then I'm receiving an Unauthorized response from the api. I think the problem is that because I've executed the GETASYNC() without the adding some headers. Can you help me to find a way on how to add headers before I execute the GETASYNC().
public HttpClient webApiClient = new HttpClient();
public async System.Threading.Tasks.Task<ActionResult> Index()
{
var uri = new Uri("https://myURL.com/"+ transno);
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var response = await webApiClient.GetAsync(uri);
response.Headers.Add("Accept", "application/json");
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response.Headers.Add("client-id", "clientid");
response.Headers.Add("client-secret", "clientsecret");
response.Headers.Add("partner-id", "partnerid");
var result = JObject.Parse(await response.Content.ReadAsStringAsync());
}

Web API sending byte array from picture gives 404

i need help with my WebAPI. I have the following in the server :
[System.Web.Http.HttpGet]
public HttpResponseMessage Recognize(byte[] img)
{
}
And my client requests :
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost/MP.Business.Implementation.FaceAPI/");
using (var request = new HttpRequestMessage(HttpMethod.Get, client.BaseAddress))
{
var imageStream = new ByteArrayContent(pic);
var multi = new MultipartContent();
multi.Add(imageStream);
// var response = client.
HttpResponseMessage response = client.GetAsync("api/Recognition/Recognize/"+ multi).Result;
The breakpoint is not hit and i get 404.However if i send this request :
HttpResponseMessage response = client.GetAsync("api/Recognition/Recognize/").Result;
the breakpoint is hit and obviously i get error inside , because the byte[] is null(but at least it hits) . What do i do wrong ?

A connection attempt failed because the connected party did not properly -- Using HttpClient

I am receiving connection attempt failed to my web api service when I called the service from asp.net mvc application. If I call the service from a browser or from MVC application hosted on another server, it works.
It also works if I host the application in test server or development server.
It doesn't work only when I host the application on that server and call the service from the mvc app.
Any suggestion. Is something on the hosted server is blocking the connection.
using (var client = new HttpClient())
{
string baseUrlFromConfig = ConfigurationManager.AppSettings["webServiceUrl"];
client.BaseAddress = new Uri(baseUrlFromConfig);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Uri myUri = new Uri(baseUrlFromConfig + "api/account/ConfirmEmail?e=" + e + "&code=" + code, UriKind.Absolute);
var response = client.GetAsync(myUri).Result;
if (response.IsSuccessStatusCode)
{
string responseString = response.Content.ReadAsStringAsync().Result;
}
}

Authorization header with HttpGet with proxy

I am using HttpGet with a proxy setting. The call works for endpoints that don't need an authorization token. For services that need a token, I use setHeader(), but see an error saying auth token missing.
What am I missing ?
HttpGet get = new HttpGet(endPoint);
get.setHeader("Authorization", token);
HttpHost proxy = new HttpHost("exampleproxy.com",8080);
RequestConfig config = RequestConfig.custom() .setProxy(proxy).build();
get.setConfig(config);
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
CloseableHttpResponse response = httpclient.execute(get);
BufferedReader responseReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

Resources