I have to call an external rest service from wpf application. I do not have any control on the service. When I make a request to the service using a rest client (e.g. Postman) it works fine i.e. success is returned. A post call is done to this URL:
http://mydomain.com:38080/workshop/rest/login?username=usr&password=pwd
You can see that I have to pass username and password in querystring.
But when I do the same from my application the service returns failed. Here is my code:
string EndPoint = "http://mydomain.com:38080/workshop/";
string parameters = "username=usr&password=pwd";
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri(EndPoint);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsync("rest/login", new StringContent(parameters)).Result;
Task task = response.Content.ReadAsStreamAsync().ContinueWith(t =>
{
var stream = t.Result;
using (var reader = new StreamReader(stream))
{
responseValue = reader.ReadToEnd();
}
});
task.Wait();
It seems that the service is expecting parameters in querystring whereas my code is passing them in request header. So how do I pass the parameters in querystring in a post call?
Related
I am trying to call a Odata service from the C# application. I have called the rest services before and consumed the responses in the C#, and trying Odata for the first time. Below is the Code I am using
using (var client = new HttpClient())
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
Uri uri = new Uri(BaseURL);
client.BaseAddress = uri;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = client.GetAsync(uri).Result;
var responsedata = await response.Content.ReadAsStringAsync();
I am using the same URL and the credentials in PostMan and it returns the response. But throws error i the code, is there something different we need to follow calling a Odata services.Please help with this
It is recommended to use a library to access OData. There are at least a couple of libraries that you can choose from, such as:
https://www.nuget.org/packages/Microsoft.OData.Client/ (OData v4)
https://www.nuget.org/packages/Microsoft.Data.OData/ (OData v1..3)
I'm integrating a service that returns a key when I a GET request to a URL that is in the following format:
https://username:password#service.com/refresh.key
When I access the URL in my browser, it returns the new key as expected, by when I do a GET request using HttpClient I get a 401.
HttpClient _client = new HttpClient();
var response = await _client.GetAsync(#"https://username:password#service.com/refresh.key"); // Returns a 401
I think it has something to do with the '#' in the URL, but I'm not sure how to fix it, I tried replacing it with '%40', but when I do that I get a UriFormatException.
Does anyone know how to do this?
You should modify Authorization header of HttpClient, can you try the code below;
HttpClient _client = new HttpClient();
byte[] usernamePasswordBytes = Encoding.ASCII.GetBytes("user:pass");
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(usernamePasswordBytes));
var response = await _client.GetAsync(#"https://service.com/refresh.key");
PS: Such username:pass#domain.com requests are BasicAuthentication request so in fact you try to make basic authentication request.
Hope this works for you
You don't need to provide credentials in url. Instead you can do:
using (var handler = new HttpClientHandler {Credentials = new NetworkCredential("username", "password")}) {
using (HttpClient _client = new HttpClient(handler)) {
var response = await _client.GetAsync(#"https://service.com/refresh.key");
}
}
I have a page on a site designed for adding a certain entity. What I'm trying to do is to add this entity using C# HttpClient. My sequence of steps looks like this:
First I'm authenticate using the client:
public static async Task<CookieCollection> WebPortalLogin(string baseURI, string phoneNo, string pin)
{
var cookies = new CookieContainer();
var handler = new HttpClientHandler()
{
CookieContainer = cookies
};
var client = new HttpClient(handler);
var content = new FormUrlEncodedContent(new[]{
new KeyValuePair<string,string>("page","login"),
new KeyValuePair<string,string>("noStaticBox",""),
new KeyValuePair<string,string>("username",phoneNo),
new KeyValuePair<string,string>("password",pin),
new KeyValuePair<string,string>("login","Увійти"),
new KeyValuePair<string,string>("_reqNo","0"),
});
var response = await client.PostAsync(baseURI, content);
response.EnsureSuccessStatusCode();
var stringResponse = response.Content.ReadAsStringAsync().Result;
var cookieJar = cookies.GetCookies(new Uri(baseURI));
return cookieJar;
}
Then I send POST request to edit page with all data I want to save:
public static async Task<HttpResponseMessage> AddCar(string baseURI, string phoneNo, CookieCollection cookieJar, string carNo, string owner)
{
var cookieContainer = new CookieContainer();
var client = new HttpClient(new HttpClientHandler() { CookieContainer = cookieContainer });
var content = new FormUrlEncodedContent(new[]{
new KeyValuePair<string,string>("carNo",carNo),
new KeyValuePair<string,string>("userName",owner),
new KeyValuePair<string,string>("page","carNumbers"),
new KeyValuePair<string,string>("submit","Додати"),
new KeyValuePair<string,string>("operation","addCar"),
new KeyValuePair<string,string>("_reqNo","0")
});
cookieContainer.Add(new Uri(baseURI), cookieJar);
var response = await client.PostAsync(baseURI, content);
var stringResponse = response.Content.ReadAsStringAsync().Result;
return response;
}
However, this POST request does nothing, and in response I have this very edit page, although when I add this entity in normal way (via web site), I get empty response and entity is successfully saved. Already checked cookies - they're all right. The only thing I can think of is request headers, but successful POST has only regular ones, like Accept, Accept-Encoding etc. What are my possible mistakes and how can I get it posted? Note: all connections use HTTPS.
ok, after messing a couple of hours I've discovered that the problem is in one of request parameters and not related to httpclient
I need to add http headers to the HttpClient before I send a request to a web service. How do I do that for an individual request (as opposed to on the HttpClient to all future requests)? I'm not sure if this is even possible.
var client = new HttpClient();
var task =
client.GetAsync("http://www.someURI.com")
.ContinueWith((taskwithmsg) =>
{
var response = taskwithmsg.Result;
var jsonTask = response.Content.ReadAsAsync<JsonObject>();
jsonTask.Wait();
var jsonObject = jsonTask.Result;
});
task.Wait();
Create a HttpRequestMessage, set the Method to GET, set your headers and then use SendAsync instead of GetAsync.
var client = new HttpClient();
var request = new HttpRequestMessage() {
RequestUri = new Uri("http://www.someURI.com"),
Method = HttpMethod.Get,
};
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
var task = client.SendAsync(request)
.ContinueWith((taskwithmsg) =>
{
var response = taskwithmsg.Result;
var jsonTask = response.Content.ReadAsAsync<JsonObject>();
jsonTask.Wait();
var jsonObject = jsonTask.Result;
});
task.Wait();
When it can be the same header for all requests or you dispose the client after each request you can use the DefaultRequestHeaders.Add option:
client.DefaultRequestHeaders.Add("apikey","xxxxxxxxx");
To set custom headers ON A REQUEST, build a request with the custom header before passing it to httpclient to send to http server.
eg:
HttpClient client = HttpClients.custom().build();
HttpUriRequest request = RequestBuilder.get()
.setUri(someURL)
.setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.build();
client.execute(request);
Default header is SET ON HTTPCLIENT to send on every request to the server.
I am creating a Metro App that makes a HTTP Post request to a webserver to obtain JSON data. Initially I use the same code to logon to the web server and the HTTP Post request returns fine. It is only later that I run into an issue where the code hangs when I call the SendAsync() method.
I used wireshark to view the network traffic and I did see the server returning a response. So I am not sure why the call is not completing. Anyone have any ideas?
Here is the code I am using:
var httpHandler = new HttpClientHandler();
httpHandler.CookieContainer = __CookieJar;
var httpClient = new HttpClient(httpHandler);
UserAgentDetails userAgent = UserAgentDetails.GetInstance();
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent.UserAgentString);
foreach (string key in __colHeaders)
httpClient.DefaultRequestHeaders.Add(key, __colHeaders[key]);
var content = new StringContent(postData);
if (contentType != null && contentType.Length > 0)
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
var requestMsg = new HttpRequestMessage(HttpMethod.Post, new Uri(url));
requestMsg.Content = content;
requestMsg.Headers.TransferEncodingChunked = true;
var responseMsg = await httpClient.SendAsync(requestMsg);
// no return from method after logon