Answered by:
Avoid exception while using Web APIs

Question
-
User1447694405 posted
Hi All,
My task is take users from one API and Put in someother API. before puting into new API i need to check this user is existing or not. But while i checking if its not exsting getting exception like 404. How can i avoid this. if not there i need to add this data as new uservar http = (HttpWebRequest)WebRequest.Create(new Uri("https://actjira.atlassian.net/rest/api/2/user?username="+ username)); http.ContentType = "application/json"; http.Method = "GET"; http.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(String.Format("{0}:{1}", "username", "password")))); HttpWebResponse response = http.GetResponse() as HttpWebResponse; // from here i getting exception
Monday, January 18, 2016 10:21 AM
Answers
-
User-782957977 posted
Looks like rest service (https://actjira.atlassian.net/rest/api/2/user) is returning HttpStatusCode.NotFound when user is not found. HttpWebRequest will throw exception if response has NotFound status code. You need to use either try catch with WebRequest or use HttpClient
WebRequest
try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); } catch (System.Net.WebException ex) { var response = ex.Response as HttpWebResponse; if (response!= null && response.StatusCode == HttpStatusCode.NotFound) { } }
HttpClient
using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = client.GetAsync("url").Result; if (response.IsSuccessStatusCode) { string responseString = response.Content.ReadAsStringAsync().Result; } else if (response.StatusCode == HttpStatusCode.NotFound) { } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 19, 2016 5:14 AM -
User-2057865890 posted
Hi binumon03,
You could try to change the response type. The following code is for your reference.
using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate })) { client.BaseAddress = new Uri("https://localhost:44300/"); //client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(String.Format("{0}:{1}", "Admin", "123")))); HttpResponseMessage response = client.GetAsync("api/values?name=1").Result; //response.EnsureSuccessStatusCode(); string result = response.Content.ReadAsStringAsync().Result; Console.WriteLine("Result: " + result); }
Best Regards,
Chris Zhao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 20, 2016 8:13 AM
All replies
-
User541108374 posted
Hi,
what is the exception you're seeing? Can you call the REST API from a tool like Fiddler or Postman? If so, compare to what you're sending with those tools. Perhaps you're missing something or put in something too much.
Grz, Kris.
Monday, January 18, 2016 10:58 AM -
User1447694405 posted
Thanks for your reply
Below i pasting exception i getting
An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code
Additional information: The remote server returned an error: (404) Not Found.
When i copy paste same url in browser i getting correct error message like user not existing
Monday, January 18, 2016 11:50 AM -
User541108374 posted
Hi,
An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code
Put a try catch statement around your code and in the exception you could get more information from it if that's what you're after.
The 404 seems a bit puzzling as it states that it doesn't find the endpoint. Can you check the url you're using and the url in the browser are exactly the same? Debug your code and copy paste the generated url into your browser to see it.
Grz, Kris.
Monday, January 18, 2016 12:49 PM -
User-782957977 posted
Looks like rest service (https://actjira.atlassian.net/rest/api/2/user) is returning HttpStatusCode.NotFound when user is not found. HttpWebRequest will throw exception if response has NotFound status code. You need to use either try catch with WebRequest or use HttpClient
WebRequest
try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); } catch (System.Net.WebException ex) { var response = ex.Response as HttpWebResponse; if (response!= null && response.StatusCode == HttpStatusCode.NotFound) { } }
HttpClient
using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = client.GetAsync("url").Result; if (response.IsSuccessStatusCode) { string responseString = response.Content.ReadAsStringAsync().Result; } else if (response.StatusCode == HttpStatusCode.NotFound) { } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 19, 2016 5:14 AM -
User-2057865890 posted
Hi binumon03,
You could try to change the response type. The following code is for your reference.
using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate })) { client.BaseAddress = new Uri("https://localhost:44300/"); //client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(String.Format("{0}:{1}", "Admin", "123")))); HttpResponseMessage response = client.GetAsync("api/values?name=1").Result; //response.EnsureSuccessStatusCode(); string result = response.Content.ReadAsStringAsync().Result; Console.WriteLine("Result: " + result); }
Best Regards,
Chris Zhao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 20, 2016 8:13 AM