I have an issue - my http request to TFS always returns "401" status code.
Request code below:
public async Task TfsPostRequest(string url, string body, string username, string password)
{
using (var client = _httpClient.CreateClient("TfsClient"))
{
try
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
var content = new StringContent(body);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
request.Content = content;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
request.Headers.Authorization = new AuthenticationHeaderValue("Ntlm", Base64Encode(username, password));
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
...
}
catch (JsonException ex)
{
...
}
catch (Exception ex)
{
...
}
}
}
Base64Encode method:
public string Base64Encode(string username, string password)
{
string creds = string.Format("{0}:{1}", username, password);
byte[] bytes = Encoding.Unicode.GetBytes(creds);
return Convert.ToBase64String(bytes);
}
HttpClient configuration code:
builder.Services.AddHttpClient("TfsClient", client => {
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.DefaultRequestHeaders.Connection.Add("keep-alive");
});
When i try to execute simular one request using
credentialCache.Add(new Uri(uri), "Ntlm", validatingNetworkCredential);
with HttpWebRequest it works correct.
My code will be executing with different user credentials and i want to use http client for that task instead of HttpWebRequest. Is it possible?
Related
I would like to ask help how can I fix the issue in the header of my httpclient request.
This is ebay restful api in creating a fulfillment shipment. I am able to create in Postman but when I tried it in VS, it won't work with error bad request. Screenshot below using postman.
Codes below in ASP.NET
private HttpClient CreateHttpClient()
{
var client = new HttpClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string baseAddress = WebApiBaseAddress;
client.Timeout = new TimeSpan(0, 5, 59);
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", _cred.eBayToken));
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
return client;
}
public HttpResponseMessage PostHttpResponse(string requestUri, object data)
{
var stringPayload = JsonConvert.SerializeObject(data);
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
httpContent.Headers.Add("Content-Language", "en-US");
using (var client = CreateHttpClient())
{
try
{
HttpResponseMessage response = client.PostAsJsonAsync(requestUri, httpContent).Result;
if (response.IsSuccessStatusCode)
{
return response;
}
else
{
GetErrorsResponse(response);
throw new HttpRequestException(string.Format("There was an exception trying to post a request. response: {0}", response.ReasonPhrase));
}
}
catch (HttpRequestException ex)
{
throw ex;
//return null;
}
}
}
I was able to fix the issue by not converting the request to json but send as object. Though the error provided is very generic and could not identify the main issue. Upon asking to someone has experienced in ebay integration, the main issue is to provide all the needed in the headers.
public HttpResponseMessage PostHttpResponse(string requestUri, object data)
{
using (var client = CreateHttpClient())
{
try
{
HttpResponseMessage response = client.PostAsJsonAsync(requestUri, data).Result;
if (response.IsSuccessStatusCode)
{
return response;
}
else
{
GetErrorsResponse(response);
throw new HttpRequestException(string.Format("There was an exception trying to post a request. response: {0}", response.ReasonPhrase));
}
}
catch (HttpRequestException ex)
{
throw ex;
//return null;
}
}
}
And in the httpclient needs to add the header.
private HttpClient CreateHttpClient()
{
var client = new HttpClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string baseAddress = WebApiBaseAddress;
if (string.IsNullOrEmpty(baseAddress))
{
throw new HttpRequestException("There is no base address specified in the configuration file.");
}
client.Timeout = new TimeSpan(0, 5, 59);
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", _cred.eBayToken));
client.DefaultRequestHeaders.Add("Accept-Language", "en-US");
client.DefaultRequestHeaders.Add("Accept-Charset", "utf-8");
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("LegacyUse", "true");
return client;
}
I've written a python function to move a router's IO port via a HTTP post request with Basic Authentivation. This works fine. But now I'd like to implement the sam with C#.
Here is my python function:
def io_on(ip='192.168.2.1', username='adm', password='123456'):
if not isinstance(ip, str):
print('not string')
try:
payload ='_ajax=1&_web_cmd=%21%0Aio%20output%201%20on%0A'
r = requests.post('http://{}/apply.cgi'.format(ip), auth=HTTPBasicAuth(username, password), data=payload, timeout=3)
if r.status_code == 200:
print('{} : IO ON'.format(ip))
elif r.status_code == 401:
print('{} : Auth error'.format(ip))
else:
print(r.status_code)
except Exception as e:
print(e)
I've experimented with NetWorkCredentials with no success.
Something like this :
try
{
string username = "adm", password = "123456";
string payload = "http://192.168.2.1/apply.cgi/?_ajax=1&_web_cmd=%21%0Aio%20output%201%20on%0A";
HttpClient client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes($"{username}:{password}");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = await client.GetAsync(payload);
HttpContent content = response.Content;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Success");
}
else if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Console.WriteLine("Auth error");
}
else
{
Console.WriteLine(response.StatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
Here's my way to make POST with basic authentication.
var authValue = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{login}:{password}")));
using (var client = new HttpClient() { DefaultRequestHeaders = { Authorization = authValue } })
{
HttpResponseMessage response = client.PostAsync("https://localhost:44396/Documentation/All?pageNumber=0&pageSize=10", httpContent).Result;
if (response.IsSuccessStatusCode)
{
response = await response.Content.ReadAsStringAsync();
}
}
My Code is as follow:-
i have no idea why i am receiving this message, please help. Right now
i am using sandbox account to test this. I have generated the data i.e. sample data from API explorer and i am passing it as a parameter as Json.
public bool GeneratePayment(string JsonData)
{
var principal = User as ClaimsPrincipal;
Session["realmId"] = "XXXXXX";
if (Session["realmId"] != null)
{
string realmId = Session["realmId"].ToString();
string qboBaseUrl = ConfigurationManager.AppSettings["QBOBaseUrl"];
//add qbobase url and query
string uri = string.Format("{0}/v3/company/{1}/invoice", qboBaseUrl, realmId);
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "XXXX");
//Here i am getting waiting for activation
var result = client.PostAsync(uri, new StringContent(JsonData, System.Text.Encoding.UTF8, "application/json"));
return true;
}
catch (Exception ex)
{
return false;
}
}
else
return false;
}
Has to do with the Task associated with PostAsync.
The GeneratePayment method needs to be made async and client.PostAsync needs to be awaited as well
public async Task<bool> GeneratePayment(string JsonData) {
var principal = User as ClaimsPrincipal;
Session["realmId"] = "XXXXXX";
if (Session["realmId"] != null) {
string realmId = Session["realmId"].ToString();
string qboBaseUrl = ConfigurationManager.AppSettings["QBOBaseUrl"];
//add qbobase url and query
string uri = string.Format("{0}/v3/company/{1}/invoice", qboBaseUrl, realmId);
try {
var client = http.Value; //singleton http client
var result = await client.PostAsync(uri, new StringContent(JsonData, System.Text.Encoding.UTF8, "application/json"));
return true;
} catch (Exception ex) {
return false;
}
}
else
return false;
}
//Singleton lazy loaded HttpClieny
static Lazy<HttpClient> http = new Lazy<HttpClient>(() => {
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "XXXX");
return client;
});
I am trying to consume [this API] (https://learn.microsoft.com/en-us/rest/api/vsts/release/approvals/update). Below is my code, but i am getting 400 bad request.
HttpContent z = new StringContent("{\"status\": \"approved\",\"comments\": \"" + Request.QueryString["comment"].ToString() + "\"}", Encoding.UTF8, "application/json");
public static async Task PatchAsync(Uri requestUri, HttpContent content)
{
try
{
using (HttpClient client = new HttpClient())
{
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, requestUri)
{
Content = content
};
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", "XXXXXXXXX"))));
//using (HttpResponseMessage response = await client.PostAsync(requestUri, content))
using (HttpResponseMessage response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
respApproval = responseBody;
}
}
}
catch (Exception ex)
{
respApproval = ex.ToString();
}
}
Since you only provide part of the code, I posted my code (which can update approvals successfully) below for your refernce:
public static async void ApproveRelease()
{
try
{
var username = "alternate auth or PAT";
var password = "password";
string accountName = "https://account.visualstudio.com";
string projectName = "projectname";
int approvalid = id;
var approveReleaseUri = "https://accountname.vsrm.visualstudio.com/projectname/_apis/release/approvals/approvlID?api-version=4.1-preview.3";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", username, password))));
var method = new HttpMethod("PATCH");
string approvveReleaseMetaData = "{\"status\":\"approved\", \"comments\":\"Good to go\"}";
var request = new HttpRequestMessage(method, string.Format(approveReleaseUri, accountName, projectName, approvalid, apiVersion))
{
Content = new StringContent(approvveReleaseMetaData, Encoding.UTF8, "application/json")
};
using (HttpResponseMessage response = client.SendAsync(request).Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
By referring the blog Using ReleaseManagement REST API’s.
Note: you can only update a release approval which status is pending. If you try to update a release approval which approval status is approved or rejected, you will also get the 400 bad request response.
The server team says that everything is fine with this HTTPS website, however the below code when using HTTPClient gives back "The request was aborted: Could not create SSL/TLS secure channel." error and at the same time works perfectly fine by returning back the intended data.
public async Task<string> GetDataAsync(string baseAddress, string relativeAddress, string token)
{
string responseBodyAsText = string.Empty;
string responseStatusCode = string.Empty;
try
{
using (HttpClientHandler handler = new HttpClientHandler())
{
handler.Credentials = new NetworkCredential(token, token);
using (HttpClient client = new HttpClient(handler))
{
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, relativeAddress))
{
using (HttpResponseMessage response = await client.SendAsync(request))
{
responseBodyAsText = await response.Content.ReadAsStringAsync();
responseStatusCode = ReturnStatusCode(response);
}
}
}
}
}
catch (Exception e)
{
responseBodyAsText = BuildErrorMessage("response", "error", e.InnerException.Message, responseStatusCode);
}
return responseBodyAsText;
}
In order to approach this issue in a different way I wrote another method which uses HTTPWebRequest and HTTPWebResponse. This code never gave me this error until now "The request was aborted: Could not create SSL/TLS secure channel."; However I wanted to post this out to find what would be the possible issue?
public async Task<string> GetDataAsync(string url, string token)
{
string responseString = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.Trim());
request.Method = "GET";
request.ContentType = "application/xml";
request.Headers[HttpRequestHeader.Authorization] = GetAuthorizationText(token.Trim());
var response = (HttpWebResponse)await request.GetResponseAsync();
if (response != null)
{
var responseStream = new StreamReader(response.GetResponseStream());
responseString = await responseStream.ReadToEndAsync();
if (responseString.Trim().Length == 0)
throw new InvalidDataException("No Content");
}
else
throw new NullReferenceException("Reponse is null");
}
catch (WebException we)
{
responseString = BuildErrorMessage("response", "error", we.Response.Headers["Status"].ToString());
}
catch (Exception ex)
{
WriteDiagnosticsInformation(ex);
responseString = BuildErrorMessage("response", "error", ex.Message);
}
return responseString;
}
Please do share your thoughts, however I doubt may be its the issue with using HTTPClient.
Try to put this
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
before everything
I was the same problem.
put the below code before WebRequest.Create
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = (wsender, certificate, chain, erros) => {
return true;
};