How to post data using HttpClient? (an answer than actually works) - c#

There is another question about this, but it doesn't have a functioning solution at the end, and the only good answer, for some reason, doesn't work, not for the guy who ask it, not for me either.
This such question is here:
How to post data using HttpClient?
Given the corresponding aclarations, this is the code I have so far:
The methods to call the method who connects with the web server:
private void Button_Click(object sender, System.EventArgs e)
{
//. . . DO SOMETHING . . .
PopulateListView();
//. . . DO SOMETHING ELSE . . .
}
private void PopulateListView()
{
//. . . DO SOMETHING . . .
list = await "http://web.server.url".GetRequest<List<User>>();
//. . . DO SOMETHING ELSE . . .
}
The method than connects with the web server:
public async static Task<T> SendGetRequest<T>(this string url)
{
try
{
var uri = new Uri(url);
HttpClient client = new HttpClient();
//Preparing to have something to read
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("OperationType", "eaf7d94356e7fd39935547f6f15e1c4c234245e4")
});
HttpResponseMessage response = await client.PostAsync(uri, formContent);
#region - - Envio anterior (NO FUNCIONO, SIN USO) - -
//var stringContent = new StringContent("markString");
//var sending = await client.PostAsync(url, stringContent);
//MainActivity.ConsoleData = await client.PostAsync(url, stringContent);
#endregion
//Reading data
//var response = await client.GetAsync(url);
var json = await response.Content.ReadAsStringAsync();
MainActivity.ConsoleData = json.ToString();
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
}
catch(Exception ex)
{
Console.WriteLine("Error: "+ex.ToString());
return default(T);
}
}
You maybe guessed it, but I'm trying to make a method that send some data (through POST) called "markString" to a web-server than receive it and, depending of the "markString" it returns certain json Objects.
This web server is already working properly (I tested it out with some plug-in, it work like it should)
This method is supposed to send the "markString" and receive the data back so then i can use it in the app.
I'm making a Xamarin Android application.
Also have in mind than I don't have any connection problem at all, in fact the app is sending data in an excellent matter when I try to do it using web client, but I want it to send it using HttpClient.
The problem
The code is not returning anything. Any request for information, clarification, question, constructive comments or anything than can lead to an answer would be greatly appreciated too.
Thanks in advance.

Most deadlock scenarios with asynchronous code are due to blocking further up the call stack.
By default await captures a "context" (in this case, a UI context), and resumes executing in that context. So, if you call an async method and the block on the task (e.g., GetAwaiter().GetResult(), Wait(), or Result), then the UI thread is blocked, which prevents the async method from resuming and completing.

void Main()
{
var test = SendGetRequest("http://www.google.com");
test.Dump();
}
public async static Task<string> SendGetRequest(string url)
{
try
{
var uri = new Uri(url);
HttpClient client = new HttpClient();
//Preparing to have something to read
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("OperationType", "eaf7d94356e7fd39935547f6f15e1c4c234245e4")
});
HttpResponseMessage response = await client.PostAsync(uri, formContent);
#region - - Envio anterior (NO FUNCIONO, SIN USO) - -
//var stringContent = new StringContent("markString");
//var sending = await client.PostAsync(url, stringContent);
//MainActivity.ConsoleData = await client.PostAsync(url, stringContent);
#endregion
//Reading data
//var response = await client.GetAsync(url);
var json = await response.Content.ReadAsStringAsync();
return json;
}
catch (System.Exception ex)
{
Console.WriteLine("Error: " + ex.ToString());
return string.Empty;
}
}

Related

Issue with HttpClient.PostAsJsonAsync Not Working from ASPX

I have created an ASP.NET Core 2.1 service and I can call it just fine from a console application. However, when I use the very same code to call it from an ASPX page, it does not return an answer. It just never goes past _client.PostAsJsonAsync and seems to run forever. It should only take a handful of seconds to go through that line. Any idea on what I am missing?
List<OutputAddress> outputAddresses = RunAsync(inputAddresses).GetAwaiter().GetResult();
static async Task<List<OutputAddress>> RunAsync(List<InputAddress> addresses)
{
// Update port # in the following line.
_client.BaseAddress = new Uri("http://<servername>/GeocodeAPI/");
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.Timeout = new TimeSpan(0, 10, 0);
try
{
HttpResponseMessage response = await _client.PostAsJsonAsync("api/Geocode/Addresses", addresses);
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<List<OutputAddress>>(result);
}
else
return null;
}
catch (Exception e)
{
return null;
}
}
=======================
Huge thank you to Nkosi for their response. Here's what I had to change:
Function calling ASP.NET Core service
static async Task<List<OutputAddress>> RunAsync(List<InputAddress> addresses)
{
_client.BaseAddress = new Uri("http://<servername>/GeocodeAPI/");
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.Timeout = new TimeSpan(0, 10, 0);
try
{
HttpResponseMessage response = await _client.PostAsJsonAsync("api/Geocode/Addresses", addresses);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<OutputAddress>>(result);
}
else
return null;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
Function calling the above function (RunAsync): added async keyword
private async void ReadCsvFile(string filepath)
{
...
List<OutputAddress> outputAddresses = await RunAsync(inputAddresses);
...
}
Added Async="true" to aspx code:
<%# Page ... Async="true" %>
Mixing async-await and blocking calls like .Result; can cause deadlocks
await the calls to getting the content
var result = await response.Content.ReadAsStringAsync();
Also, if using async-await, go async all the way.
List<OutputAddress> outputAddresses = await RunAsync(inputAddresses);
Reference Async/Await - Best Practices in Asynchronous Programming
Beware of how you're using asynchronous code in ASP.NET Web Forms.
You need to use page async tasks.
That being said, nothing in async changes the HTTP protocol and, if you want some behavior on the client side, you need to implement it on the client, as Anu showed.

HttpClient won't work

So I'm trying to send a multiform POST to API with http client but it's just hang there indefinetly. I test this code in console and it worked as it should, but then I try to run it like this for the UI
private static async Task<string> ApiTask(...)
{
var SourceStream = File.Open(imgpath,FileMode.Open);
var FileStreamContent = new StreamContent(SourceStream);
FileStreamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
var client = new HttpClient();
using (var formData = new MultipartFormDataContent())
{
formData.Add(new StringContent("this is a test"),"comment");
formData.Add(new StringContent("Command: detect"),"message");
formData.Add(fileStreamContent, "image","image.jpg");
var response = await client.PostAsync(url,formData).ConfigureAwait(false);
var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return responseString
}
}
And I'm calling it from the EventHandler
public async void buttnclck(object sender, EventArgs e)
{
var task = await ApiTask(...);
lblresult.Text = task;
}
but as I said the code just stay in de .PostAsync line indefinetly or when a System.Threading.Task.TaskCanceledException is thrown.
So what I missing here? I thing I was handeling the async/await methods just fine but it's clear I'm not. I tried also with .Result but it won't work even and would throw System.AggregateException. So please help, been trying to make it work modifying the code as other suggested responses but still not working
EDIT:
after couple of hours debugging and searching I find out my problem relies in formData.Add(FileStreamContent, "image","image.jpg"); maybe I'm not serializing the image correctly? How can I fix this??

UWP 10 C# HttpResponseMessage task freezes

I couldn't find from search anyone having similar issues so:
I'm trying to get XML from server with HttpClient, but my UI freezes weirdly at line "task.Wait()". This is the code:
public void register() {
String data = "register=" + (accountName) + "&email0=" +
(email) + "&email1=" + (otherEmail);
var task = MakeRequest(data);
task.Wait(); //freezes here
var response = task.Result;
String resultstring = response.Content.ReadAsStringAsync().Result;
}
private static async Task<System.Net.Http.HttpResponseMessage> MakeRequest(String data)
{
var content = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded");
var httpClient = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage responseMessage=null;
try
{
responseMessage = await httpClient.PostAsync(server, content);
}
catch(Exception ex)
{
responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
}
return responseMessage;
}
Any help is greatly appreciated!
It's not freezing weirdly at all - it's freezing entirely reasonably.
You're calling task.Wait(), which stops your UI thread from doing any more work until that task has completed. However, that task itself needs to get back to the UI thread when the task returned by httpClient.PostAsync completes, in order to continue with the rest of your async method.
Basically, you shouldn't use Task.Wait() or Task.Result unless you know for sure that the task itself won't be blocked waiting to continue on the thread you're currently executing on.
Ideally, your register method (which should be Register to follow .NET naming conventions) should be asynchronous as well, so you can await the task returned by MakeRequest.
Additionally, you should probably change the way MakeRequest awaits the task - as the rest of that method doesn't really need to run on the UI thread, you can use:
responseMessage = await httpClient.PostAsync(server, content).ConfigureAwait(false);
Finally, this line:
responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
... will throw a NullReferenceException if it ever executes. If an exception is thrown, responseMessage will still be null.
Answer of Jon Skeet solved my problem, and here is the working code, if some other beginner is having same problems.
public async void Register{
String data = "register=" + (accountName) + "&email0=" +
(email) + "&email1=" + (otherEmail);
var task = await MakeRequest(data);
String resultstring = taskContent.ReadAsStringAsync().Result;
}
private static async Task<System.Net.Http.HttpResponseMessage> MakeRequest(String data)
{
var content = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded");
var httpClient = new System.Net.Http.HttpClient();
return await httpClient.PostAsync(server, content).ConfigureAwait(false);
}
Thank you very much!

Making Http Call from c#

There is a classic asp application which makes calls to .shtml files using AspHttp.Conn. It makes the request by appending all the input and also reads the response by reading the values in the response by length.
Here is the example
strMessage= "test.shtml"
Set HttpObj = Server.CreateObject("AspHTTP.Conn")
HttpObj.Url = url & strMessage
HttpObj.PostData = "testarea=" & strRequestData
HttpObj.TimeOut = 60
HttpObj.RequestMethod = "post"
strResponseData = HttpObj.GetURL
Response.Write Mid(strResponseData,3,1)
Response.Write Mid(strResponseData,4,3)
If I need to rewrite this, what will be the best way to do this. I will be using MVC and will be rewriting the UI. what will be the best approach to make httpcall from c#?. The backend to which the request will be sent will not be changed. Please suggest.
Using .Net Framework 4.5 you can make request as
public static async Task<int> HtmlLoadAsync(string url/*, bool addUserAgent = false*/)
{
try
{
var client = new HttpClient();
//if (addUserAgent) OPTIONAL
//{
// client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
//}
//client.Timeout = TimeOut;
var response = client.GetStringAsync(url); //here you can change client method according to your required outpu
var urlContents = await response;
// process urlContents now
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return 0;
}
Now call it as
private async void Process()
{
await HtmlLoadAsync("http://....");
}
Note: You must have to add reference System.Net.Http

Catch exceptions while making web api calls

I am making a lot of web api calls in my c# code. I don't know how to catch errors. Suppose, internet connection isn't working, then my code shows runtime error. How to I properly put them into try catch block, What's the general rule. All the articles I found were on how to throw back the error and error message.
example API calls:
WebResponse webResponse = webRequest.GetResponse();
string res = webResponse.ToString();
Also
using (var client = new HttpClient())
client.BaseAddress = new Uri(CairoBaseUrl);
var getStringTask = client.GetStringAsync(requestUrl);
response = await getStringTask;
And,
HttpResponseMessage response = await client.PostAsync(
url,requestContent);
if you are calling asp.net web api i would suggest you to use HttpClient which is done for that purposes
try
{
HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Console.WriteLine(responseBody);
}
catch(HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ",e.Message);
}
This is example from MSDN how to deal with exceptions using http client
in you example you have
using (var client = new HttpClient())
client.BaseAddress = new Uri(CairoBaseUrl);
var getStringTask = client.GetStringAsync(requestUrl);
response = await getStringTask;
But it wont work since await operator can be used only with methods marked async so it should be
var getStringTask = await client.GetStringAsync(requestUrl);

Categories