indy10 idhttp can not use SetHeaders this method? lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- greenzyzyzy · 2015-03-01 12:01 indy10 idhttp can not use SetHeaders this method? but indy9 have it(idhttp1.request.SetHeaders), what can i do now? ---------------------------------------------------------------------- ChrisF · 2015-03-01 12:41 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 · 2015-03-02 05:00 Quote from: ChrisF on March 01, 2015, 01:41:30 pmWhat 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 · 2015-03-02 13:00 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 · 2015-03-03 12:57 Quote from: ChrisF on March 02, 2015, 02:00:20 pmCookies 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 · 2015-03-03 13:05 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. ---------------------------------------------------------------------- greenzyzyzy · 2015-03-03 13:14 Quote from: ChrisF on March 03, 2015, 02:05:49 pmThere 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 · 2015-03-03 13:35 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 · 2015-03-03 13:43 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 · 2015-03-03 13:49 Quote from: ChrisF on March 03, 2015, 02:43:47 pmI 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 · 2015-03-03 13:54 Quote from: greenzyzyzy on March 03, 2015, 02:49:27 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. Quote from: greenzyzyzy on March 03, 2015, 02:49:27 pmif i want to use cookiemanager.how can i do? Have you looked at my last sample code : http://forum.lazarus.freepasc al.org/index.php/topic,27550.msg170532.html#msg170532  after the "***Edit ***" section  ? Any problem with it ? ---------------------------------------------------------------------- greenzyzyzy · 2015-03-04 04:48 Quote from: ChrisF on March 03, 2015, 02:54:35 pmQuote from: greenzyzyzy on March 03, 2015, 02:49:27 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. Quote from: greenzyzyzy on March 03, 2015, 02:49:27 pmif i want to use cookiemanager.how can i do? Have you looked at my last sample code : http://forum.lazarus.freepasc al.org/index.php/topic,27550.msg170532.html#msg170532  after the "***Edit ***" section  ? Any problem with it ? ok ,i will have a test, thank you!