Delphi HttpWebRequest with custom fields

In previous post, I demonstrated how to program a small utility to crawl a website content (HttpWebRequest with GET method in Delphi .net). In this tutorial, I will show you an advanced step to add custom HTTP fields to HttpWebRequest header. One common field that you will need to add when working with AJAX application is X-Requested-With:XMLHttpRequest. To add custom fields, you need to assign a variable (type WebHeaderCollection) to a local variable, and add more fields to this variable, then you’re good to go.

For example, I’d like to add X-Requested-With:XMLHttpRequest to my HttpWebRequest header, I will do the following steps

  1. MyHeader: WebHeaderCollection; //Declare a local variable as WebHeaderCollection
  2. MyHeader := MyWebRequest.Headers; //Assign MyHeader variable to current HttpWebRequest
  3. MyHeader.Add(‘X-Requested-With:XMLHttpRequest); //Add custom field to HttpWebRequest header
Follow the rules
You can add custom fields to HttpWebRequest header, but not all custom headers are accepted. You might encounter Run-time-errors with some headers. Test each header to make sure it’s applicable.

One more thing to mention, there is no blank space between the header and the value. X-Requested-With:XMLHttpRequest is good, but not X-Requested-With: (space) XMLHttpRequest

And here is the full sample code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function LoadWebSolo(website: string): string;
var MyURI: uri;
  MyWebRequest: HttpWebRequest;
  MyWebResponse: HttpWebResponse;
  MyStream: stream;
  MyStreamReader: streamreader;
  MyHeader: WebHeaderCollection; //Declare MyHeader as a local variable
begin
  try
    MyURI := uri.Create(website);
    MyWebRequest := HttpWebRequest.CreateDefault(MyURI) as HttpWebRequest;
    MyHeader := MyWebRequest.Headers; //Assign MyHeader to MyWebRequest header part
    MyHeader.Add('X-Requested-With:XMLHttpRequest'); // Add a new custom field to MyWebRequest header by using MyHeader as a local variable
    MyWebRequest.Accept := 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
    MyWebRequest.Useragent := 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10';
    MyWebRequest.KeepAlive := true;
    MyWebRequest.ContentType := 'application/x-www-form-urlencoded';
    MyWebRequest.AllowAutoRedirect := true;
 
    MyWebResponse := HttpWebResponse(MyWebRequest.GetResponse);
    MyStream := MyWebResponse.GetResponseStream;
    MyStreamReader := StreamReader.Create(MyWebResponse.GetResponseStream);
    if MyStreamReader <> nil then
      Result := MyStreamReader.ReadToEnd;
 
  except
    on e: exception do
    ShowMessage('Error message: ' + e.message);
  end;
end;
function LoadWebSolo(website: string): string;
var MyURI: uri;
  MyWebRequest: HttpWebRequest;
  MyWebResponse: HttpWebResponse;
  MyStream: stream;
  MyStreamReader: streamreader;
  MyHeader: WebHeaderCollection; //Declare MyHeader as a local variable
begin
  try
    MyURI := uri.Create(website);
    MyWebRequest := HttpWebRequest.CreateDefault(MyURI) as HttpWebRequest;
    MyHeader := MyWebRequest.Headers; //Assign MyHeader to MyWebRequest header part
    MyHeader.Add('X-Requested-With:XMLHttpRequest'); // Add a new custom field to MyWebRequest header by using MyHeader as a local variable
    MyWebRequest.Accept := 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
    MyWebRequest.Useragent := 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10';
    MyWebRequest.KeepAlive := true;
    MyWebRequest.ContentType := 'application/x-www-form-urlencoded';
    MyWebRequest.AllowAutoRedirect := true;

    MyWebResponse := HttpWebResponse(MyWebRequest.GetResponse);
    MyStream := MyWebResponse.GetResponseStream;
    MyStreamReader := StreamReader.Create(MyWebResponse.GetResponseStream);
    if MyStreamReader <> nil then
      Result := MyStreamReader.ReadToEnd;

  except
    on e: exception do
    ShowMessage('Error message: ' + e.message);
  end;
end;

You can apply this method for both GET and POST HttpWebRequest.

About The Author

Hao Nguyen

Hao Nguyen

Hello! I'm Hao Nguyen and I'm currently working as a Network Engineer for a small firm in Houston. I enjoy writing technical documents and blog, such as PlanTutorials.com

Leave a Reply