Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

HTTP Web Response and Request Best Practices to Avoid Common Errors

$
0
0

I am currently investigating an issue with our web authentication server not working for a small subset of users. My investigating led to attempting to figure out the best practices for web responses.

The code below allows me to get a HTTP status code for a website.

string statusCode;

CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);

// Allow auto-redirect
myHttpWebRequest.AllowAutoRedirect = true;

// make sure you have a cookie container setup. 
// Probably not saving cookies and getting caught
// in a redirect loop.
myHttpWebRequest.CookieContainer = cookieContainer;
WebResponse webResponse = myHttpWebRequest.GetResponse();

statusCode = "Status Code: " + (int)((HttpWebResponse)webResponse).StatusCode + ", " + ((HttpWebResponse)webResponse).StatusDescription;

Through out my investigation, I encountered some error status codes, for example, the "Too many automatic redirections were attempted" error. I fixed this by having a Cookie Container, as you can see above.

My question is - what are the best practices for requesting and responding to HTTP requests?

I know my hypothesis that I'm missing crucial web request methods (such as implementing a cookie container) is correct, because that fixed the redirect error. 

I suspect my customers are having the same issue as a result of using our software to authenticate with our server/URL. I would like to avoid as many web request issues as much as possible.

Thank you.



Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>