Check uri is valid without causing it to download using HttpClient - c#

How to Verify if the download uri is valid without causing client to download content?

create client
Use client.GetAsync(uri, HttpComplettionOption) api
use ResponseHeadersRead
this will only check if the uri is valid without downloading actual content.
HttpClient client = new HttpClient();
var downloadUri = new Uri("https://domainame/someblobcontent.zip");
Task<HttpResponseMessage> response = null;
try
{
using (response = client.GetAsync(downloadUri, HttpCompletionOption.ResponseHeadersRead))
{
if(response.Result.IsSuccessStatusCode)
{
Console.WriteLine("uri is valid, got response code {0}", response.Result.StatusCode);
}
else
{
Console.WriteLine("uri is not valid, got response code {0}", response.Result.StatusCode);
}
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}

Related

C# Unable to view Errors on API Response just throws an Exception to Try/Catch

I am writing a program to check check if a Voucher number is Valid and I am finding it difficult to extract the Error Message from a REST API which I am working with.
C# is pretty new to me as normally VB.net but covering for someone at the moment.
Basically I have a HttpWebReqest and HttpWebResponse objects and using the below code I am making a successful request and getting a response just fine.
When everything goes well there are no problems, but for example if a voucher was invalid or the site was invalid I should get a response saying this, as I do in Postman, see below for example.
{
"message": "The given data was invalid.",
"errors": {
"voucher_no": [
"Sorry, that voucher number is invalid."
]
}
}
Instead I get thrown to the Try/Catch.. with the Exception
Error Message Error 422 unprocessable entity,
with no further details or object to check for the real message above?
try
{
using (HttpWebResponse response = mywebrequest.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
{
// I am unable to get to this part of the Code to process the Error because Try/Catch is executed instead ...
}
else
{
Stream dataStream1 = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream1);
responseFromServer = reader.ReadToEnd();
}
}
}
catch (WebException ex)
{
msgbox = new MsgBox_UI("Error", "Web Server Returned an Error", "There is a problem with this Voucher. It may be Expired or invalid at this time.", 1, false, 28);
msgbox.ShowDialog();
break;
}
If any one out there has any ideas as to how I can get this working it would be a great help.
This is by design, GetResponse will throw a WebException (1) when the request returns an 'unsuccessful' status code.
You can check the Status property on the WebException to get the statuscode
and the Response property for the response of the webserver.
The first thing it's better to use HttpClient class instead.
this code should work for you (if not let me know) :
private async Task<string> GetExtensionToken()
{
string url = "https://YourApi.com";
try
{
var httpclient = new HttpClient();
using (HttpResponseMessage response = httpclient.GetAsync(url).Result)
{
using (HttpContent content = response.Content)
{
string result = content.ReadAsStringAsync().Result;
string Is_Not_Valid = "invalid";
if (result.Contains(Is_Not_Valid))
{
string token = "Whatever you want to extract if error page" ;
return token;
}
else
{
string token = "Whatever you want to extract if succeeded" ; return token;
}
}
}
}
catch (Exception ex)
{
return "Error from catch ";
}
}
Usage:
private async void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = await GetExtensionToken();
}
Ok so I took the advice of Peter above and decided to use HttpClient().
However I actually went with Restharp and installed the Nuget Package RestSharp into my Project. (Main reason is POSTMAN Code Snippet gave me the exact code to use.
Then it worked like a dream.
I am not doing it Async so here is what I found fixed my problem after adding
using RestSharp;
var client = new RestClient("https://api.voucherURL.uk/redeem");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Smart-Auth", "sk_xxxxxxxxxxxxxxxxxxxxxxxxxxx");
request.AddHeader("Accept", "application/json");
request.AddParameter("application/json", "{\n \"voucher_no\":\"JY584111E3\",\n \"site_id\": 14\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Not able to download file using C# from a web api which works using postman

I have recently started working with web api's.
I need to download a file in C# project from a web api, which works fine when I hit the web api using postman's send and download option. Refer to the image, also please check the response in header's tab. This way, I am able to directly download the file to my computer.
I want to do the same from my C# project, I found following two links which shows how to download a file from web api.
https://code.msdn.microsoft.com/HttpClient-Downloading-to-4cc138fd
http://blogs.msdn.com/b/henrikn/archive/2012/02/16/downloading-a-google-map-to-local-file.aspx
I am using the following code in C# project to get the response:
private static async Task FileDownloadAsync()
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Accept", "text/html");
try
{
// _address is exactly same which I use from postman
HttpResponseMessage response = await client.GetAsync(_address);
if (response.IsSuccessStatusCode)
{
}
else
{
}
}
catch (Exception)
{
throw;
}
}
}
However I am not getting the response at all (before I can start to convert the response to a file), please check the error message coming:
What am I missing here, any help would be appreciated.
As the (500s) error says - it's the Server that rejects the request. The only thing I see that could cause an issues is the charset encoding. Yours is the default UTF-8. You could try with other encodings.
Below method uses:
SSL certificate (comment out code for cert, if you don't use it)
Custom api header for additional layer of security (comment out Custom_Header_If_You_Need code, if you don't need that)
EnsureSuccessStatusCode will throw an error, when response is not 200. This error will be caught in and converted to a human readable string format to show on your screen (if you need to). Again, comment it out if you don't need that.
private byte[] DownloadMediaMethod(string mediaId)
{
var cert = new X509Certificate2("Keystore/p12_keystore.p12", "p12_keystore_password");
var handler = new WebRequestHandler { ClientCertificates = { cert } };
using (var client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Custom_Header_If_You_Need", "Value_For_Custom_Header");
var httpResponse = client.GetAsync(new Uri($"https://my_api.my_company.com/api/v1/my_media_controller/{mediaId}")).Result;
//Below one line is most relevant to this question, and will address your problem. Other code in this example if just to show the solution as a whole.
var result = httpResponse.Content.ReadAsByteArrayAsync().Result;
try
{
httpResponse.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
if (result == null || result.Length == 0) throw;
using (var ms = new MemoryStream(result))
{
var sr = new StreamReader(ms);
throw new Exception(sr.ReadToEnd(), ex);
}
}
return result;
}
}
Once you have your http response 200, you can use the received bytes[] as under to save them in a file:
using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
fs.Write(content, 0, content.Length);
}
Your request header says that you accept HTML responses only. That could be a problem.

Get URL using HttpClient C# .NET

I am trying to get the URL of a page using HttpClient. I've previously only used HttpWebRequest, but I need to make this an async method.
In the code below, myUri always returns null which results in throwing an exception when I try to handle it later on.
Is the location header the wrong thing to be using?
string myUrl = "http://www.example.com/";
Uri myUri= new Uri(myUrl);
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(myUri))
{
if (response.IsSuccessStatusCode)
{
myUri= response.Headers.Location;
Debug.WriteLine("True "+ myUri);
}
else {
Debug.WriteLine("False " + myUri);
}
}
It's because HttpClient will automatically follows redirects. If you need the URL a page redirects to, you need to stop it from automatically following:
Change your code to the following:
string myUrl = "http://www.example.com/";
Uri myUri= new Uri(myUrl);
HttpClientHandler httpClientHandler = new HttpClientHandler();
httpClientHandler.AllowAutoRedirect = false;
using (HttpClient client = new HttpClient(httpClientHandler))
Here is an async way to resolve the final redirect URL:
public static async Task<string> ResolveFinalRedirectAsync(string url)
{
try
{
var req = WebRequest.CreateHttp(url);
req.AllowAutoRedirect = true;
var res = await req.GetResponseAsync();
return res.ResponseUri.AbsoluteUri;
}
catch
{
Console.WriteLine("Couldn't resolve '{0}'", url);
}
return null;
}
See #Rob's answer about AllowAutoRedirect.
Once you do that, note
The line
if (response.IsSuccessStatusCode)
evaluates to false if you receive a HTTP 301 redirect (anything outside of the 200-299 range)
A value that indicates if the HTTP response was successful. true if HttpStatusCode was in the Successful range (200-299); otherwise false.
(source)
OK thanks I'm trying to get redirected URLs anyway
If you prevent automatically following redirects, you will get an HTTP response in the 3xx range for the redirect. Your check for valid codes will have to be modified accordingly.

How to deal with HTML error codes and timeout from HttpClient()

I'm using HttpClient to connect to a server (see simplified code below). I cant figure out how I would respond to HTML error codes (e.g. 403) and timeouts so I can report what the result is.
When I encounter a 403 error code an error pop-up occurs in Visual Studio. But I can figure out how I convert this into try in the code. i.e. is the name of the exception present in the error pop-up?
using System.Net.Http;
HttpClient client = new HttpClient();
var response = client.PostAsync(dutMacUrl, null).Result;
var result = response.Content.ReadAsStringAsync().Result;
you can use async/await feature to simplify your code and avoid using Result.
for example
public async Task<string> Foo(string uri)
{
var client = new HttpClient();
try
{
var response = await client.PostAsync(uri, null);
}
catch (Exception ex)
{
//here you handle exceptions
}
// use this if (response.StatusCode != HttpStatusCode.OK) { do what you want }
// or this if (response.IsSuccessStatusCode) { do what you want }
var result = await response.Content.ReadAsStringAsync();
return result;
}
If you are using webAPI another option is to use IHttpActionResult
public object IHttpActionResult mymethod()
{
// instantiate your class or object
IEnumerable<yourClass> myobject = new IEnumerable<yourClass>();
// assuming you want to return a collection
try
{
// do stuff
// handle dto or map result back to object
return Ok(myobject)
}
catch(Exception e)
{
// return a bad request if the action fails
return BadRequest(e.Message)
}
}
This would allow you to make a call to your api endpoint and either return a successful response with the updated object or return a bad request if the endpoint fails.

Why HttpResponse always return 404 status code

I am trying to send request to http://localhost/apptfg/get_biography_group?nombre_grupo=fondoflamenco which is a php based webservice that access to mysql database and retrieve information about the group but I always get a 404 not found when I execute the application in my windows phone 8 device, however when I debug the url in fiddler I get the right result which must be {"success":1,"group":[{"nombre_grupo":"fondoflamenco","anyo_creacion":"2006","descripcion":"Fondo Flamenco Flamenco is a group formed by three young Sevillian. Astola Alejandro Soto, Antonio Sanchez and Rafael Ruda M.R","musicos":"Rafael Ruda,Antonio Manuel Rios,"}]}
this is the HttpClient code I use in my application:
public async Task<string> makeHttpRequest(string group_name)
{
var resultstring = String.Empty;
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "text/html");
try
{
resultstring = await client.GetStringAsync(new Uri("http://localhost/apptfg/get_group_biography.php?nombre_grupo=" + group_name));
client.Dispose();
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
return resultstring;
}

Categories

Resources