Recent

Author Topic: indy10 idhttp can not use SetHeaders this method?  (Read 10493 times)

greenzyzyzy

  • Full Member
  • ***
  • Posts: 249
indy10 idhttp can not use SetHeaders this method?
« on: March 01, 2015, 01:01:05 pm »
indy10 idhttp can not use SetHeaders this method?
but indy9 have it(idhttp1.request.SetHeaders), what can i do now?

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #1 on: March 01, 2015, 01:41:30 pm »
What are you trying to do ? Add your own header data ? Modify some ?

You can use different fields to modify some common one. For instance:
Code: [Select]
  MyIdHTTP.Request.UserAgent:=MyUserAgent;
  MyIdHTTP.Request.Connection:='keep-alive';

And you can use CustomHeaders for your specific ones. For instance:
Code: [Select]
MyIdHTTP.Request.CustomHeaders.Add('DNT: 1');

greenzyzyzy

  • Full Member
  • ***
  • Posts: 249
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #2 on: March 02, 2015, 06:00:52 am »
What are you trying to do ? Add your own header data ? Modify some ?

You can use different fields to modify some common one. For instance:
Code: [Select]
  MyIdHTTP.Request.UserAgent:=MyUserAgent;
  MyIdHTTP.Request.Connection:='keep-alive';

And you can use CustomHeaders for your specific ones. For instance:
Code: [Select]
MyIdHTTP.Request.CustomHeaders.Add('DNT: 1');


well ,i just want to add cookies info.and send it to the server, what can i do?it seems this way is failed.

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #3 on: March 02, 2015, 02:00:20 pm »
Cookies are now handled trough the CookieManager class.

You must create a new CookieManager instance (at design time or at runtime), and affect it to the CookieManager property of your IdHTTP object.

Then cookies can then be added with:
  procedure AddServerCookie(const ACookie: String; AURL: TIdURI);
  procedure AddServerCookies(const ACookies: TStrings; AURL: TIdURI);

For instance (design time sample):
- add an IdHTTP (called MyIdHTTP) to a new form Form1,
- add an IdCookieManager (called MyIdCookieManager - TIdCookieManager is in "Indy Misc Protocols" tab),
- affect MyIdCookieManager to the CookieManager property of MyIdHTTP,
- add a pushbutton,
- use the following test code
Code: [Select]
uses ..., IdURI, ...;

function AddCookie(const sData, sURL: string; const MyIdCookieManager: TIdCookieManager): boolean;
var MyIdURI: TIdURI;
begin
  result := true;
  MyIdURI := TIdURI.Create(sURL);
  try
    MyIdCookieManager.AddServerCookie(sData, MyIdURI);
  except
    result := false;
  end;
  MyIdURI.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var MyCookies: string;
var i: integer;
begin
  if not AddCookie('MyCookieValue=MyValue', 'http://www.mywebsite.com/', MyIdHTTP.CookieManager) then
    ShowMessage('Error when adding my cookie')
  else
    begin
      MyCookies := '';
      for i:=0 to Pred(MyIdHTTP.CookieManager.CookieCollection.Count) do
        MyCookies := MyCookies + MyIdHTTP.CookieManager.CookieCollection.Cookies[i].ClientCookie+'~';
      ShowMessage('MyCookies = ' + MyCookies);
    end;
end;


greenzyzyzy

  • Full Member
  • ***
  • Posts: 249
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #4 on: March 03, 2015, 01:57:47 pm »
Cookies are now handled trough the CookieManager class.

You must create a new CookieManager instance (at design time or at runtime), and affect it to the CookieManager property of your IdHTTP object.

Then cookies can then be added with:
  procedure AddServerCookie(const ACookie: String; AURL: TIdURI);
  procedure AddServerCookies(const ACookies: TStrings; AURL: TIdURI);

For instance (design time sample):
- add an IdHTTP (called MyIdHTTP) to a new form Form1,
- add an IdCookieManager (called MyIdCookieManager - TIdCookieManager is in "Indy Misc Protocols" tab),
- affect MyIdCookieManager to the CookieManager property of MyIdHTTP,
- add a pushbutton,
- use the following test code
Code: [Select]
uses ..., IdURI, ...;

function AddCookie(const sData, sURL: string; const MyIdCookieManager: TIdCookieManager): boolean;
var MyIdURI: TIdURI;
begin
  result := true;
  MyIdURI := TIdURI.Create(sURL);
  try
    MyIdCookieManager.AddServerCookie(sData, MyIdURI);
  except
    result := false;
  end;
  MyIdURI.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var MyCookies: string;
var i: integer;
begin
  if not AddCookie('MyCookieValue=MyValue', 'http://www.mywebsite.com/', MyIdHTTP.CookieManager) then
    ShowMessage('Error when adding my cookie')
  else
    begin
      MyCookies := '';
      for i:=0 to Pred(MyIdHTTP.CookieManager.CookieCollection.Count) do
        MyCookies := MyCookies + MyIdHTTP.CookieManager.CookieCollection.Cookies[i].ClientCookie+'~';
      ShowMessage('MyCookies = ' + MyCookies);
    end;
end;





custom cookies is no effect in more than two times,


  //IdHTTP1.AllowCookies:=false;
  IdHTTP1.Request.CustomHeaders.Text:=UTF8TOAnsi('Cookie: myuser=12345; mypass=abcdefg');
  IdHTTP1.Get('http://www.baidu.com');                    <=============here  is no problem
  IdHTTP1.Get('http://www.baidu.com');                    <=============but here is no effect


i have no ideas,

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #5 on: March 03, 2015, 02:05:49 pm »
There is no "custom cookies".

There is :
- CustomHeader: for your own header data (NOT including your own cookies);
- and CookieManager: for all the cookies, which means those coming from the server(s) and eventually yours.

So, do use CookieManager in your case.

**Edit**  Sample (not tested and without ant try/except block):

Code: [Select]
uses ..., IdCookieManager, IdUri;

var MyIdCookieManager: TIdCookieManager;

// Create and affect a Cookie Manager to your IdHTTP object
//    (To be called once, at the beginning)
procedure SetMyCookieManager();
begin
  MyIdCookieManager := TIdCookieManager.Create(nil);
  Form1.IdHTTP1.CookieManager := MyIdCookieManager;   // Adapt  Form1.IdHTTP1 to your own control
end;

// Release the Cookie Manager
//    (To be called once, at the end)
procedure FreeMyCookieManager();
begin
  Form1.IdHTTP1.CookieManager := nil;
  MyIdCookieManager.Free;
end;

// Add cookie values
procedure AddMyCookie(const sURL, sData: string);
var MyIdUri: TIdUri;
begin
  MyIdUri := TIdUri.Create(sURL);
  MyIdCookieManager.AddServerCookie(sData, MyIdURI);
  MyIdUri.Free;
end;


...
  AddMyCookie('http://www.baidu.com', 'myuser=12345');          // Or use a TStringList
  AddMyCookie('http://www.baidu.com', 'mypass=abcdefg');        //
...

**Edit 2** Some bug fixes in the sample code.
« Last Edit: March 03, 2015, 06:55:00 pm by ChrisF »

greenzyzyzy

  • Full Member
  • ***
  • Posts: 249
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #6 on: March 03, 2015, 02:14:20 pm »
There is no "custom cookies".

There is :
- CustomHeader: for your own header data (NOT including your own cookies);
- and CookieManager: for all the cookies, which means those coming from the server(s) and eventually yours.

So, do use CookieManager in your case.

IdHTTP1.Request.CustomHeaders.Text:=('Cookie: myuser=12345; mypass=abcdefg');
Here is my own cookies which want send to server,i can see it in the sent packets in the first time.
but i can not see it in the sent packets in more than sent two times.

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #7 on: March 03, 2015, 02:35:10 pm »
Cookies are parsed by Indy, and probably overwritten by those coming from the server. That's why I think you can't use the CustomHeaders property.

Cookies must be used with the CookieManager in Indy: this is -most probably- the only way (if you don't want to do all the job by yourself).

Please, have a look at my last sample code ("Edit" in my former post).

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #8 on: March 03, 2015, 02:43:47 pm »
I don't know Baidu, but are you sure you can connect to this site by only adding these cookies ?

Usually, you'll have to connect to the site though a "special" request, and often a session Id is created by the site.

Sometimes, the user's data are added to the cookies, but they are absolutely not the way to connect to it (i.e. you can't just add them to your cookies to connect to this site).

But, once again, i don't know how Baidu is working...

greenzyzyzy

  • Full Member
  • ***
  • Posts: 249
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #9 on: March 03, 2015, 02:49:27 pm »
I don't know Baidu, but are you sure you can connect to this site by only adding these cookies ?

Usually, you'll have to connect to the site though a "special" request, and often a session Id is created by the site.

Sometimes, the user's data are added to the cookies, but they are absolutely not the way to connect to it (i.e. you can't just add them to your cookies to connect to this site).

But, once again, i don't know how Baidu is working...


i found the problem,that is if add cookiemanager no effect in more than two times, if not add cookiemanager,it is no problem
if i want to use cookiemanager.how can i do?

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #10 on: March 03, 2015, 02:54:35 pm »

i found the problem,that is if add cookiemanager no effect in more than two times, if not add cookiemanager,it is no problem

(Probably) Because Indy keep an internal trace of all the cookies when a cookie manager is used.

if i want to use cookiemanager.how can i do?

Have you looked at my last sample code : http://forum.lazarus.freepascal.org/index.php/topic,27550.msg170532.html#msg170532 
after the "***Edit ***" section  ?

Any problem with it ?

greenzyzyzy

  • Full Member
  • ***
  • Posts: 249
Re: indy10 idhttp can not use SetHeaders this method?
« Reply #11 on: March 04, 2015, 05:48:21 am »

i found the problem,that is if add cookiemanager no effect in more than two times, if not add cookiemanager,it is no problem

(Probably) Because Indy keep an internal trace of all the cookies when a cookie manager is used.

if i want to use cookiemanager.how can i do?

Have you looked at my last sample code : http://forum.lazarus.freepascal.org/index.php/topic,27550.msg170532.html#msg170532 
after the "***Edit ***" section  ?

Any problem with it ?


ok ,i will have a test, thank you!

 

TinyPortal © 2005-2018