C3rd
C# WebRequest: Headers pitfall
Posted: 4 Jun 2020, 6:36am - Thursday
I have written my own WebRequest wrapper and it took me ages why I am getting a response of bad request. Hours of debugging, I realize that I have overwritten my Content-Type header with my Authentication-Authorization entries.
the issue:
WebRequest request = WebRequest.Create(url);
request.Method = method_request;
request.ContentType = "application/x-www-form-urlencoded";
/**
* dont add content-type header on authenticate
*/
if (!url.Contains("/authenticate"))
{
WebHeaderCollection whc = new WebHeaderCollection();
whc.Add("Auth-username", authorizationEntity.getUsername());
whc.Add("Auth-session-key", authorizationEntity.getSessionKey());
request.Headers = whc;
}
if (method_request.ToUpper() == "POST")
{
Stream stream = request.GetRequestStream();
byte[] postArray = Encoding.UTF8.GetBytes(flatten_post_data);
stream.Write(postArray, 0, postArray.Length);
stream.Close();
}
string Result;
try
{
Console.WriteLine(request.Headers.ToString());
StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream());
Result = sr.ReadToEnd();
} catch(WebException e)
{
Console.WriteLine(e.Message);
Result = "[]";
}
the correct approach:
WebRequest request = WebRequest.Create(url);
request.Method = method_request;
/**
* dont add content-type header on authenticate
*/
if (!url.Contains("/authenticate"))
{
WebHeaderCollection whc = new WebHeaderCollection();
whc.Add("Auth-username", authorizationEntity.getUsername());
whc.Add("Auth-session-key", authorizationEntity.getSessionKey());
request.Headers = whc;
}
request.ContentType = "application/x-www-form-urlencoded";
if (method_request.ToUpper() == "POST")
{
Stream stream = request.GetRequestStream();
byte[] postArray = Encoding.UTF8.GetBytes(flatten_post_data);
stream.Write(postArray, 0, postArray.Length);
stream.Close();
}
string Result;
try
{
Console.WriteLine(request.Headers.ToString());
StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream());
Result = sr.ReadToEnd();
} catch(WebException e)
{
Console.WriteLine(e.Message);
Result = "[]";
}
So that's it. lesson learn! LOL
Search
Categories
Related Links
- Apache Web Server
- ASP Immigration Services Ltd.
- CentOS Linux
- Color Scheme Picker
- CSS Button Generator
- Deviant Art Account
- Don Tabaniag - GraphiCult
- Jerson Maglasang Weblog
- Joshua Manlunas
- Mark "mungkey" Vicente
- Matt Arnilo Baluyos
- MySQL
- Paul Labis
- PHP
- PrendStar
- Ridvan Baluyos
- Scott Davies
- SEO Fart XML-RPC Ping Tool
- Stripes Generator
- The Hero
- Ubuntu Linux
- Vim Carlo Nabora (DA)
- Wella Maria Hong
- What is my IP?
- Xavier University - Ateneo de Cagayan
- Zabyer Community