programmerdelphi2k 237 Posted November 20, 2022 (edited) Hello girls and boys, I'm try to do a little app to read, post and delete my message in a forum. In forum, i'm like a "Moderator" (but not ADMIN privilegies! then I can post, edit, delete message (mine or from others)! the forum use old tech on server: -- Server: Microsoft-IIS/7.5 -- X-Powered-By: ASP.NET -- Content-Type: text/html; charset=utf-8 -- IP: v4 The answer from Indy "GET" method is a text/html with all necessary to show a "HTML page", but I would just get some data in this page, not whole page! -- NONE API is available to help me! then, just read the page-response!!! ------------------------------------------------------------------- Im using Delphi 10 and "INDY" (TIdHttp class). ``` AHttp := TIdHTTP.Create(nil); try try AHTTP.ReadTimeout := 10000; AHTTP.Request.ContentType := 'application/json'; // maybe other configurations, I dont know? AHTTP.Request.CharSet := 'utf-8'; AHTTP.Request.Accept := '*/*'; AHTTP.Request.BasicAuthentication := true; AHTTP.Request.Username := 'my user name'; AHTTP.Request.Password := 'my password'; ... // method GET... Memo1.Text := Http.Get(HTTP_DEFAULTPAGE); // **GET it's OK for now!!!** ... I would like use others: POST, UPDATE and DELETE except // showmessage... end; finally AHTPP.Free; end ----------------------------------- > resulted GET: 1) for now, I can get the "response" without problem, but the resulted is a "HTML" text. 2) I would like that was in JSON to catch the "key:value", but unfortunatelly ... many tags HTML default ... 3) If was possible "JSON pair", it would help me! it's possible? 4) if not, then is there some way to better get the "message titles", at least? ------------------------------------- NOTE: in my IdHttp, I always send my "username + password" like above! I would like edit a message: ---------------------------- 1) I type the topicID 2) I get the messsage (posted) in "edit" mode 3) I change the message content 4) I post the new message content I would like "add" a message: ----------------------------- I would like delete the message: ---------------------------------------- 1) I type the topicID 2) I send a "delete" command 3) then, the message would be deleted NOTE: -- Currently, to Delete any messages, I have that: 1) Edit the message to see the "button DELETE" (there is not a button before "Edit message" 2) on source of the page I see: <input type="submit" name="del" class="button" value='....'> to delete the current message for now it's only this. thanks Edited November 20, 2022 by programmerdelphi2k Share this post Link to post
Remy Lebeau 1394 Posted November 23, 2022 On 11/20/2022 at 9:49 AM, programmerdelphi2k said: I'm try to do a little app to read, post and delete my message in a forum. In forum, i'm like a "Moderator" (but not ADMIN privilegies! then I can post, edit, delete message (mine or from others)! Since you are asking how to implement this with Indy, you should have posted this in the Indy group: https://en.delphipraxis.net/forum/35-indy/ On 11/20/2022 at 9:49 AM, programmerdelphi2k said: The answer from Indy "GET" method is a text/html with all necessary to show a "HTML page", but I would just get some data in this page, not whole page! You don't really have a choice in that matter. This is an all-or-nothing kind of deal. You are basically simulating a web browser, so you are going to receive whatever a web browser would receive to render data onscreen for the user to interact with. What you decide to do with that data is up to you. But if this is not the kind of data you want, then you should check if the website in question offers a separate REST API to access the data in another format that is more machine-oriented rather than user-oriented. On 11/20/2022 at 9:49 AM, programmerdelphi2k said: AHTTP.Request.ContentType := 'application/json'; // maybe other configurations, I dont know? AHTTP.Request.CharSet := 'utf-8'; You are not sending any data to the server, so you shouldn't be assigning values to those properties at all. On 11/20/2022 at 9:49 AM, programmerdelphi2k said: ... I would like use others: POST, UPDATE and DELETE And? What is your actual question about them? You are going to have to look at the underlying HTTP requests that a normal web browser submits for those actions, and then replicate them in your code accordingly. On 11/20/2022 at 9:49 AM, programmerdelphi2k said: > resulted GET: 1) for now, I can get the "response" without problem, but the resulted is a "HTML" text. Because that is what you are asking for from the server. You are requesting data from a URL of an HTML webpage, so HTML is what you get back. On 11/20/2022 at 9:49 AM, programmerdelphi2k said: 2) I would like that was in JSON to catch the "key:value", but unfortunatelly ... many tags HTML default ... Unless the server explicitly supports that operation for the URL in question (ie, via a URL query parameter or the 'Accept' header, etc), then you won't be able to get what you want. Either find another URL that gives you the data you want in the format you want, or else you will just have to deal with the HTML you are given. On 11/20/2022 at 9:49 AM, programmerdelphi2k said: 3) If was possible "JSON pair", it would help me! it's possible? Only if the server supports giving out JSON. But, there is no way for anyone here to tell you how to do that, since you haven't even explained which website you are trying to access in the first place. On 11/20/2022 at 9:49 AM, programmerdelphi2k said: 4) if not, then is there some way to better get the "message titles", at least? If all you have is HTML, then you are just going to have to parse it to extract what you want from it. On 11/20/2022 at 9:49 AM, programmerdelphi2k said: I would like edit a message: ---------------------------- 1) I type the topicID 2) I get the messsage (posted) in "edit" mode 3) I change the message content 4) I post the new message content You probably don't need step 2 at all. Chances are, you can likely just look at the HTML of the edit webpage to determine the URL that it submits to, and then you can make your code Post() to that same URL with the appropriate headers and data as needed. On 11/20/2022 at 9:49 AM, programmerdelphi2k said: I would like "add" a message: Again, just look at the HTML for the appropriate webpage, and then have your code post your data to the same URL. On 11/20/2022 at 9:49 AM, programmerdelphi2k said: I would like delete the message: Same as above. Determine the URL that the delete webpage submits to, and then make your code post an appropriate request to that same URL. On 11/20/2022 at 9:49 AM, programmerdelphi2k said: NOTE: -- Currently, to Delete any messages, I have that: 1) Edit the message to see the "button DELETE" (there is not a button before "Edit message" 2) on source of the page I see: <input type="submit" name="del" class="button" value='....'> to delete the current message for now it's only this. OK, so what is the problem? Most modern web browsers have a built-in debugger that can show the raw HTTP traffic. TIdHTTP is just a wrapper for the HTTP protocol, so see what kind of HTTP requests a web browser sends for the actions you are interested in, and then replicate those requests in your TIdHTTP code. Not that hard, really. Share this post Link to post
programmerdelphi2k 237 Posted November 23, 2022 12 hours ago, Remy Lebeau said: ... Remi response.... hi @Remy Lebeau first, thanks for your time. As you see, my knowledge of HTML is next to nothing, so I'll try some more information. -- I understand that: If server send (just) HTML, then, I have only HTML. OK! -- use Chrome Debug, I think that I dont know how intercept my requeriment, at all. -- to ask to see (on screen of Chrome) "one" message, I can to do this: https://<<address-from-server>>/topic.asp?topicid=nnnnn <-- ID message that I can see on Chrome URL ------ using INDY "GET" I can receive this message (in HTML text) in my MEMO, too! My big problem is (out others, like "coding" :) -- Currently, in the browser, to delete any message (SPAM), I need to enter the message to see the "DELETE" button. And, looking at the "source" of the page, by Chrome, right mouse button, I see the above mentioned instruction (<input type="submit" name="del" ...), so I thought someone could you tell me how this could be sent by INDY, or another way, because in the Chrome URL I don't see anything related to this. That is, I don't know how this is being sent to the server. Other question about "send config in REQUEST above": you say that I dont need/can send any configuration to server if I just ask something... not send something to server? is that? Share this post Link to post
programmerdelphi2k 237 Posted November 23, 2022 (edited) when clicking in "edit message" I see this in URL: https://<<addres-from-server>>/topic_edit_submit.asp in source-page I see this info: ... <table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#000000"> <form name="Form1" action="topic_edit_submit.asp" method="post" onSubmit="return check()"> <input type="hidden" name="roomid" value="1"> <input type="hidden" name="topicfatherid" value="648330"> <input type="hidden" name="topicid" value="648379"> ... <input type="submit" name="edit" class="button" value="edit"> <input type="submit" name="del" class="button" value="delete"> <input type="reset" name="reset" class="button" value="reset"> <input name="topic_title" type="hidden" value="2"> <textarea class="textarea" name="topic_content" rows="11" cols="73"> </textarea> </form> </table> ... other thing... what would be usage of "TIdMultiPartFormDataStream" in IdHTTP.POS(...)" ? it would be to send some info in "buttons" on "forms" from html page? some sample of usage? Edited November 23, 2022 by programmerdelphi2k Share this post Link to post
Remy Lebeau 1394 Posted November 24, 2022 13 hours ago, programmerdelphi2k said: -- Currently, in the browser, to delete any message (SPAM), I need to enter the message to see the "DELETE" button. And, looking at the "source" of the page, by Chrome, right mouse button, I see the above mentioned instruction (<input type="submit" name="del" ...), so I thought someone could you tell me how this could be sent by INDY, or another way, because in the Chrome URL I don't see anything related to this. That is, I don't know how this is being sent to the server. Again, simply use your browser's DEBUGGER to see the actual HTTP request that is transmitted to the server when the Delete button is clicked. And then you can replicate that same request with TIdHTTP as needed. Not all requests use URL parameters, typically requests like POST use a request body instead, which you can't see in a URL. 13 hours ago, programmerdelphi2k said: Other question about "send config in REQUEST above": you say that I dont need/can send any configuration to server if I just ask something... not send something to server? is that? I have no idea what you are saying. Share this post Link to post
Remy Lebeau 1394 Posted November 24, 2022 12 hours ago, programmerdelphi2k said: when clicking in "edit message" I see this in URL: https://<<addres-from-server>>/topic_edit_submit.asp in source-page I see this info: In TIdHTTP, that would look like this: var PostData: TStringList; begin PostData := TStringList.Create; try PostData.Add('roomid=1'); PostData.Add('topicfatherid=648330'); PostData.Add('topicid=648379'); ... // either: PostData.Add('edit=edit'); // or (depending on which button is clicked): PostData.Add('del=delete'); PostData.Add('topic_title=2'); PostData.Add('topic_content=...'); IdHTTP1.Post('https://<<addres-from-server>>/topic_edit_submit.asp', PostData); finally PostData.Free; end; end; 12 hours ago, programmerdelphi2k said: other thing... what would be usage of "TIdMultiPartFormDataStream" in IdHTTP.POS(...)" ? To submit an HTML webform whose 'enctype' attribute is set to 'multipart/form-data'. The default enctype is 'application/x-www-form-urlencoded', which is handled by the TStrings overload of TIdHTTP.Post(), as shown above. 12 hours ago, programmerdelphi2k said: it would be to send some info in "buttons" on "forms" from html page? No. The most common use of 'multipart/form-data' is when uploading files, or otherwise sending large amounts of data. Share this post Link to post
programmerdelphi2k 237 Posted November 24, 2022 (edited) @Remy Lebeau you said: On 11/20/2022 at 9:49 AM, programmerdelphi2k said: AHTTP.Request.ContentType := 'application/json'; // maybe other configurations, I dont know? AHTTP.Request.CharSet := 'utf-8'; You are not sending any data to the server, so you shouldn't be assigning values to those properties at all. ------ in F12 on Chrome, I see "POST" command was used after "Delete" button to be clicked on "edit message". but I cannot see any body reference???? Edited November 24, 2022 by programmerdelphi2k Share this post Link to post
programmerdelphi2k 237 Posted November 24, 2022 thanks by sample, I will try and let you know if ok. see you later Share this post Link to post
Remy Lebeau 1394 Posted November 24, 2022 14 minutes ago, programmerdelphi2k said: in F12 on Chrome, I see "POST" command was used after "Delete" button to be clicked on "edit message". but I cannot see any body reference???? I don't use Chrome, so I can't comment on what it does or does not show you. But a GET request does not typically have a request body, which is why I said you should not set the ContentType/CharSet properties when calling TIdHTTP.Get(). Calling TIdHTTP.Post() is a different matter. Share this post Link to post
programmerdelphi2k 237 Posted November 24, 2022 ok. talking about F12 in Chrome calls Console Debug ... same that Edge, Firefox, etc... Share this post Link to post
programmerdelphi2k 237 Posted November 24, 2022 (edited) @Remy Lebeau if I want "delete the topic", I need to do in "Browser" (not in my Indy coding) 1) find and click "edit" message. 2) into message, click "delete" button ------------------------------------------ in Indy-app in my test: // get the topic = ok! no needs nothing more for see the "message" -> it's work, I have a text-html with full-page with the message! Http.Get('https://<<server-address>/topic.asp' + '?topicid=' + LTopicID, LResponseContent); -------------- ResponseCode: 200 ResponseText: HTTP/1.1 200 OK BoundIP: BoundPort: 0 BoudnPortMin: 0 BoundPortMax: 0 ConnectTimeout: 0 ReadTimeout: 10000 AuthRetries: 0 RedirectCount: 0 MaxHeaderLines: 255 UseNagle: true Response.RawHeaders:Cache-Control: private Content-Length: 15116 Content-Type: text/html; charset=utf-8 Server: Microsoft-IIS/7.5 Set-Cookie: ASPSESSIONIDSWTRBRCT=EJ.............JA; secure; path=/ <-------- cookie X-Powered-By: ASP.NET Date: Thu, 24 Nov 2022 13:47:58 GMT ---------------- in Indy-app, I have simulate "edit... delete message", like: 1) ask to edit the message: same<<IdHttp>>Object.Get('https://<<server-address>/topic_edit.asp' + '?topicid=' + LTopicID, LResponseContent); 2) after "GET" command above, I send a "POST" command using same object "Http.xxxx" // LParams = TStringList LParams.Add('roomid=111'); LParams.Add('topicfatherid=0'); LParams.Add('topicid=' + LTopicID); LParams.Add('del="delete"'); // same<<IdHttp>>Object..Post(https://<<server-address>/topic_edit_submit.asp', LParams, LResponseContent); // DOES NOT WORKS yet! 😞 -------------- Then, if I use the same obj "Http.xxxxx" to send a "GET" command to "edit" and, after, a "POST" command to "DELETE" two commands will work in the same "session" (cookie,etc...) or each command will work in distinct session (cookie,etc...)? --- if two session, then, the second command dont will work as expected??? is it possible get the "cookie" from "GET -- edit..." to use in "POST -- delete..." command? if yes, how do it? Do you understand ? thanks for now Edited November 24, 2022 by programmerdelphi2k Share this post Link to post
Remy Lebeau 1394 Posted November 25, 2022 On 11/24/2022 at 5:52 AM, programmerdelphi2k said: 2) after "GET" command above, I send a "POST" command using same object "Http.xxxx" // LParams = TStringList LParams.Add('roomid=111'); LParams.Add('topicfatherid=0'); LParams.Add('topicid=' + LTopicID); LParams.Add('del="delete"'); You need to remove the double-quotes on the 'del' value, like I showed you eariler. On 11/24/2022 at 5:52 AM, programmerdelphi2k said: same<<IdHttp>>Object..Post(https://<<server-address>/topic_edit_submit.asp', LParams, LResponseContent); // DOES NOT WORKS yet! 😞 In what way, exactly, does it not work? Please be more specific. On 11/24/2022 at 5:52 AM, programmerdelphi2k said: Then, if I use the same obj "Http.xxxxx" to send a "GET" command to "edit" and, after, a "POST" command to "DELETE" two commands will work in the same "session" (cookie,etc...) or each command will work in distinct session (cookie,etc...)? It should be sending back the existing cookie, thus being a single session. On 11/24/2022 at 5:52 AM, programmerdelphi2k said: is it possible get the "cookie" from "GET -- edit..." to use in "POST -- delete..." command? TIdHTTP and TIdCookieManager handle that automatically for you, if you are reusing the same objects for multiple requests. Share this post Link to post
programmerdelphi2k 237 Posted November 26, 2022 (edited) 3 hours ago, Remy Lebeau said: You need to remove the double-quotes on the 'del' value, like I showed you eariler. same that exists a "blank space" between text on "button" value? like ... 'del=my delete'; <--- this way 3 hours ago, Remy Lebeau said: In what way, exactly, does it not work? Please be more specific. Basically, the "GET" works = it get the page of the topic, but "POST" (delete) dont delete the message! of course, the commands can not be accept or is wronged in my code... but basically I send the "POST" with url+topicID and param-list to "DEL" button I'll upload a TXT with all returns to "GET" and "POST" texts after send two commands: Http.GET(...) and following it Http.POST(...) in sequece in the same button-click (procedure) 3 hours ago, Remy Lebeau said: It should be sending back the existing cookie, thus being a single session. I see on returns (Get and Post) that the cookie-values is diferent each other! not the same! (same using the same Http object to send two commands in sequence) 3 hours ago, Remy Lebeau said: TIdHTTP and TIdCookieManager handle that automatically for you, if you are reusing the same objects for multiple requests. YES! I'm using the same instance Object to send "GET" and following "POST" commands. in my button procedure, I create the Http:= TIdHttp.Create(...) I send the Http.GET(...) and put the text-results in my memosLOG after "GET", I send the "POST" Http.POST(...) in sequence, and put the text-results in my memosLOG I destroy the Http object "Http.Free" for better analise, I'm send a TXT with "GET" and "POST" resulted.... I think that is better for you (and me 🙂 very thanks for you support IdHTTP_txt_infos.7z Edited November 26, 2022 by programmerdelphi2k Share this post Link to post
Remy Lebeau 1394 Posted November 26, 2022 21 hours ago, programmerdelphi2k said: same that exists a "blank space" between text on "button" value? like ... 'del=my delete'; <--- this way Yes, leave the double-quotes off. A space character will be encoded as '%20' during transmission, eg: 'del=my%20delete' 21 hours ago, programmerdelphi2k said: Basically, the "GET" works = it get the page of the topic, but "POST" (delete) dont delete the message! Because you didn't send the correct 'del' value, the server script probably didn't understand that you were asking for a delete operation. 21 hours ago, programmerdelphi2k said: of course, the commands can not be accept or is wronged in my code... but basically I send the "POST" with url+topicID and param-list to "DEL" button Again, before you can code the requests properly, you first need to study and understand what a web browser sends for each request. 21 hours ago, programmerdelphi2k said: I'll upload a TXT with all returns to "GET" and "POST" texts after send two commands I'm not at a computer right now, so I can't look at that file. I'll have to look at it later. Share this post Link to post
Remy Lebeau 1394 Posted November 28, 2022 On 11/25/2022 at 4:37 PM, programmerdelphi2k said: Basically, the "GET" works = it get the page of the topic, but "POST" (delete) dont delete the message! of course, the commands can not be accept or is wronged in my code... but basically I send the "POST" with url+topicID and param-list to "DEL" button The response is sending you back an HTML page with Javascript on it. Is it possible that the Javascript is manipulating the page in a way that affects the HTTP requests? This goes back to my earlier comments. Stop focusing on the HTML, you need to pay closer attention to the raw HTTP traffic instead. You need to use your browser's built-in debugger to see what requests are actually being sent, and what responses are actually being received. Then you can just replicate those in your code as needed. Until you can get that raw traffic that, I can't help you any further. On 11/25/2022 at 4:37 PM, programmerdelphi2k said: I see on returns (Get and Post) that the cookie-values is diferent each other! not the same! (same using the same Http object to send two commands in sequence) The POST request is sending back the same cookie that the GET response contained: From GET Response: Set-Cookie: ASPSESSIONIDQWRQBQCS=JM.................CL; secure; path=/ In POST request: Cookie: ASPSESSIONIDQWRQBQCS=JM.............CL The POST response is then sending back a new cookie: Set-Cookie: ASPSESSIONIDQWRQBQCS=MM............BP; secure; path=/ That is perfectly normal and acceptable. It is the same cookie name, just a different value. The server is well within its right to change the value of a cookie between requests. In this case, the cookie contains state information, and clearly that state is changing. Share this post Link to post
programmerdelphi2k 237 Posted November 29, 2022 3 hours ago, Remy Lebeau said: The response is sending you back an HTML page with Javascript on it. Is it possible that the Javascript is manipulating the page in a way that affects the HTTP requests? look, I really dont know... On Debug console, I go in "Network", and see basically /topic_edit_submit.asp .... topic_edit.asp... and some line with "xxxxxx.js" then, when I click in a line, the Debug show the info that I sended to you 3 hours ago, Remy Lebeau said: Until you can get that raw traffic that, I can't help you any further. unfortunatelly, I not a hacker... I'm losted :_) 3 hours ago, Remy Lebeau said: It is the same cookie name, just a different value. then, at least the basic I'm doing right? Share this post Link to post