How to properly make a http web GET request
Servers sometimes compress their responses to save on bandwidth, when this happens, you need to decompress the response before attempting to read it. Fortunately, the .NET framework can do this automatically, however, we have to turn the setting on.
Here's an example of how you could achieve that.
string html = string.Empty;
string url = @"https://api.stackexchange.com/2.2/answers?order=desc&sort=activity&site=stackoverflow";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
Console.WriteLine(html);
GET
public string Get(string uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
GET async
public async Task<string> GetAsync(string uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using(HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream))
{
return await reader.ReadToEndAsync();
}
}
POST
Contains the parameter method
in the event you wish to use other HTTP methods such as PUT, DELETE, ETC
public string Post(string uri, string data, string contentType, string method = "POST")
{
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.ContentLength = dataBytes.Length;
request.ContentType = contentType;
request.Method = method;
using(Stream requestBody = request.GetRequestStream())
{
requestBody.Write(dataBytes, 0, dataBytes.Length);
}
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
POST async
Contains the parameter method
in the event you wish to use other HTTP methods such as PUT, DELETE, ETC
public async Task<string> PostAsync(string uri, string data, string contentType, string method = "POST")
{
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.ContentLength = dataBytes.Length;
request.ContentType = contentType;
request.Method = method;
using(Stream requestBody = request.GetRequestStream())
{
await requestBody.WriteAsync(dataBytes, 0, dataBytes.Length);
}
using(HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream))
{
return await reader.ReadToEndAsync();
}
}
How to make http get request\response to site?
A client (browser) submits HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and also contains the requested content. GET - requests data from a specified resource, http://google.com - you type this url in browser and hit the enter key, it's a get request and you see a page, that the response.
Click here for details.
Use HttpClient to connect a site from your mvc controller, click here for details.
C# can't make http request from website or service
Try supplying the proxy details when making the request. Assuming you are behind a proxy.
using (var client = new System.Net.WebClient())
{
WebProxy proxy = new WebProxy("localproxyIP:8080", true);
proxy.Credentials = new NetworkCredential("domain\\user", "password");
WebRequest.DefaultWebProxy = proxy;
client.Proxy = proxy;
var values = new System.Collections.Specialized.NameValueCollection();
//values["v"] = "1";
//values["t"] = "event";
//values["tid"] = trackingID;
//values["cid"] = clientID;
//values["ec"] = eventCategory.ToString();
//values["ea"] = eventAction.ToString();
//values["el"] = eventAction.ToString();
var endpointAddress = "http://www.google-analytics.com/collect";
var response = client.UploadValues(endpointAddress, values);
var responseString = System.Text.Encoding.Default.GetString(response);
}
Making and receiving an HTTP request in C#
Making a HTTP request is very simple if you don't want to customize it: one method call to WebClient.DownloadString
. For example:
var client = new WebClient();
string html = client.DownloadString("http://www.google.com");
Console.WriteLine(html);
You will need to build the correct URL each time as per the documentation you link to.
If you use the example code above to talk to your API, html
(which is really the response data in general) will contain either XML or JSON as a string. You would then need to parse this into some other type of object tree so that you can work with the response.
HTTP client Get request within a threading parallel pool - random failures
Here's a few general guidelines:
- Avoid
async void
. Your code is currently addingasync
lambdas asAction
delegates, which end up beingasync void
. - Go
async
all the way. I.e., don't block onasync
code. In particular, don't use.Result
.
Finally, Parallel
doesn't mix with async
, because Parallel
is for CPU-bound multi-threaded parallelism, not for I/O-bound unthreaded asynchrony. The solution you need is asynchronous concurrency, i.e., Task.WhenAll
.
public override async Task Run(List<int> sensors)
{
var tasks = sensors
.Select(prd => Process(prd))
.ToList();
await Task.WhenAll(tasks);
}
private async Task<string> Process(int sensorId)
{
while (true)
{
await Task.Delay(1000);
try
{
SensorModel response = await _httpClientService.GetAsync<SensorModel>(string.Format(apiUrl, sensorId));
if (response != null)
{
if (response.Status == "success")
{
...
}
_logger.LogError($"API connection unsuccesful...");
return null;
}
_logger.LogError($"API connection failed...");
return null;
}
catch (Exception ex)
{
_logger.LogError($"Exception in retrieving sensor data {StringExtensions.GetCurrentMethod()} {ex}");
return null;
}
}
}
Making a HTTP-GET Request with Titanium.Network.createHTTPClient()
the code works fine. I tested it with a different URL.
first check the URL in a web browser to see how much it takes to load, then increase the timeout value.
if it works fine try opening the URL in the browser of the simulator, you may find that it's an IP problem
Related Topics
Remove Surrounding Whitespace from an Image
How to Return a File (Filecontentresult) in ASP.NET Webapi
Merge Multiple Lists into One List With Linq
How to Make a Soap/Wsdl Client in C#
C# Check the Empty or Null Value in All Datatable Values
C# Best Way to Run a Function Every Second, Timer VS Thread
How to Ignore Convert to Nullable in Lambda Expressions
Load Image from Resources Area of Project in C#
Read and Parse a Json File in C#
Convert Byte Array to Wav File
Cannot Access a Closed Stream of a Memorystream, How to Reopen
How to Store List of Data from Database to Arraylist or List in C#
Returning a File to View/Download in ASP.NET MVC
Best Way to Check If a Data Table Has a Null Value in It
C# - Comparing Items Between 2 Lists
C# Web API Sending Body Data in Http Post Rest Client
How to Send a File Document to the Printer and Have It Print