#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    6
    Rep Power
    0

    HTTPS and/or XML posting help please


    I would like some help or guidance on either posting the following using HTTPS or XML. I have attached both examples of code and would like the easiest way to communicate with this server. Looking at the two examples, it would seem like the HTTPS code would be easier to submit (for a novice) but I need a little help with which components to use and a hint as to what to do to transmit it. I use D7 and have used Indy's FTP client (v9.00.10) to communicate with another server but that seems a little more straight-forward than using HTTPS or XML. Any help would be greatly appreciated.

    HTTPS Code:

    Code:
    POST /secure/WebUploadFromClient HTTP/1.0
    Type: multipart/form-data; boundary=7d021a37605f0
    User-Agent: Java1.2.2
    Host: www.somehost.com
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Content-length: 698
    --7d021a37605f0
    Content-Disposition: form-data; name="message"
    <?xml version="1.0" encoding="UTF-8"?>
    <UploadRequest">
    <IdentificationHeader>
    <TradingPartnerId>222222222</TradingPartnerId>
    <UserId>xyzuser</UserId>
    <Password>jmstp567</Password>
    </IdentificationHeader>
    <Transaction>
    <Function>uploadFile</Function>
    <FileName>d:\myfile.dat</FileName>
    <FileType>ltc</FileType>
    <FileSize>45</FileSize>
    </Transaction>
    </UploadRequest>
    --7d021a37605f0
    Content-Disposition: form-data;name="textFileAttached"; filename="d:\temp\test.txt"
    Content-Type: text/plain
    File in standard X12, NCPDP 5.1 or LTC format to upload.
    data2
    data3.
    --7d021a37605f0
    XLM Code

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="FileName" type="xsd:string"/>
    <xsd:element name="FileSize" type="xsd:string"/>
    <xsd:element name="Function" type="xsd:string"/>
    <xsd:element name="IdentificationHeader">
    <xsd:complexType>
    <xsd:sequence minOccurs="1" maxOccurs="1">
    <xsd:element ref="TradingPartnerId" minOccurs="1" maxOccurs="1"/>
    <xsd:element ref="UserId" minOccurs="1" maxOccurs="1"/>
    <xsd:element ref="Password" minOccurs="1" maxOccurs="1"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="Password" type="xsd:string"/>
    <xsd:element name="TradingPartnerId" type="xsd:string"/>
    <xsd:element name="Transaction">
    <xsd:complexType>
    <xsd:sequence minOccurs="1" maxOccurs="1">
    <xsd:element ref="Function" minOccurs="1" maxOccurs="1"/>
    <xsd:element ref="FileName" minOccurs="1" maxOccurs="1"/>
    <xsd:element ref="FileType" minOccurs="1" maxOccurs="1"/>
    <xsd:element ref="FileSize" minOccurs="1" maxOccurs="1"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="UploadRequest">
    <xsd:complexType>
    <xsd:sequence minOccurs="1" maxOccurs="1">
    <xsd:element ref="IdentificationHeader" minOccurs="1" maxOccurs="1"/>
    <xsd:element ref="Transaction" minOccurs="1" maxOccurs="1"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="UserId" type="xsd:string"/>
    </xsd:schema>
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    6
    Rep Power
    0
    Any suggestions would be appreciated. Thanks
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    When posting data on https:// connections, I usually use the TWebBrowser control . Why?? Because it happens to be in the default Delphi install and is easy to use. Additionally, the pages I normally work with sometimes have some javascript and the TWebbrowser control understands them as well.

    Code:
    TfrmMain = class(TForm)
       ... snip ...
       private:
           bReadPage: boolean;    
    end;
    
    procedure TfrmMain.WebBrowserProgressChange(Sender: TObject; Progress,
    	ProgressMax: Integer);
    begin
        if Progress = -1 then
            bReadPage := true;
    end;
    
    procedure TfrmMain.PostSomeData;
    var
        strData : string;
        PostData: OleVariant;
        Headers: OleVariant;
        i : integer;
    begin
        strData := '<xml></xml>'; // Put your POST data here.
        PostData := VarArrayCreate([0, Length(strData) - 1], varByte);
        for i := 1 to Length(strData) do
            PostData[i-1] := Ord(strData[i]);
        Headers := 'Content-Type: application/x-www-form-urlencoded' + #10#13 +
        'Referer: https://somesite.com/' + #10#13;
    
        // Now navigate to the URL
        bReadPage := false;
        WebBrowser.Navigate('https://url.com/', EmptyParam, EmptyParam, PostData, Headers);
        while not bReadPage do
            Application.ProcessMessages;
    end;
    You can add your user-agent to the Headers variable in the procedure PostSomeData, the same way as I added the Referer and Content-Type. The rest of the headers should be autogenerated by the browser.
    Last edited by Scorpions4ever; August 19th, 2004 at 04:39 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    6
    Rep Power
    0
    Ok, I changed and made it a little simpler...

    Let's say I wanted to post this using your procedure:

    Code:
    POST /secure/WebDirectoryDownloadFromClient HTTP/1.0
    Content-Type: text/xml
    User-Agent: Java1.2.2
    Host: www.somesite.com
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Content-length: 708
    <?xml version="1.0" encoding="UTF-8"?>
    <DirectoryRequest">
    <IdentificationHeader>
    <TradingPartnerId>222222222</TradingPartnerId>
    <UserId>xyzuser</UserId>
    <Password>xyzpassword</Password>
    </IdentificationHeader>
    <Transaction>
    <Function>DIRLIST</Function>
    <FilesToReturnCount>50</FilesToReturnCount>
    <SelectedFileTypes>
    <FileType>997</FileType>
    <FileType>835</FileType>
    </SelectedFileTypes>
    <FileStatus>A</FileStatus>
    <FilesCreatedFromDate>20040801</FilesCreatedFromDate>
    <FilesCreatedToDate>20040831</FilesCreatedToDate>
    </Transaction>
    </DirectoryRequest>
    I used the following code but wasn't sure of I should use the #10#13 in the strData because I thought it would get rid of white-space and CR/LF anyway. I tried both ways without success.

    Code:
    procedure Tform1.PostSomeData;
    var
        strData : string;
        PostData: OleVariant;
        Headers: OleVariant;
        i : integer;
    begin
        strData := '<?xml version="1.0" encoding="UTF-8"?>'+ #10#13 +
        '<DirectoryRequest">'+ #10#13 +
        '<IdentificationHeader>'+ #10#13 +
        '<TradingPartnerId>222222222</TradingPartnerId>'+ #10#13 +
        '<UserId>xyzuser</UserId>'+ #10#13 +
        '<Password>xyzpassword</Password>'+ #10#13 +
        '</IdentificationHeader>'+ #10#13 +
        '<Transaction>'+ #10#13 +
        '<Function>DIRLIST</Function>'+ #10#13 +
        '<FilesToReturnCount>50</FilesToReturnCount>'+ #10#13 +
        '<SelectedFileTypes>'+ #10#13 +
        '<FileType>997</FileType>'+ #10#13 +
        '<FileType>835</FileType>'+ #10#13 +
        '</SelectedFileTypes>'+ #10#13 +
        '<FileStatus>A</FileStatus>'+ #10#13 +
        '<FilesCreatedFromDate>20040801</FilesCreatedFromDate>'+ #10#13 +
        '<FilesCreatedToDate>20040831</FilesCreatedToDate>'+ #10#13 +
        '</Transaction>'+ #10#13 +
        '</DirectoryRequest>'+ #10#13; // Put your POST data here.'
        PostData := VarArrayCreate([0, Length(strData) - 1], varByte);
        for i := 1 to Length(strData) do
            PostData[i-1] := Ord(strData[i]);
        Headers :=
        'Content-Type: text/xml' + #10#13 +
        'User-Agent: Java1.2.2' + #10#13 +
        'Host: www.somesite.com' + #10#13 +
        'Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2' + #10#13 +
        'Content-length: 708' + #10#13;
        // Now navigate to the URL
        bReadPage := false;
        WebBrowser1.Navigate('https://www.somesite.com', EmptyParam, EmptyParam, PostData, Headers);
        while not bReadPage do
            Application.ProcessMessages;
    end;
    I wasn't sure where (if anywhere) I should put the POST /secure/WebDirectoryDownloadFromClient HTTP/1.0 statement either.

    I keep getting the following message:

    Action canceled

    Internet Explorer was unable to link to the web page you requested. The page might be temporarily unavailable.

    If I remove the PostData and Headers the browser brings me to the main page so I know the browser component and their website are functioning.

    Please tell me what I am doing wrong and thank you for your assistance.
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    I guess you missed the part where I said
    You can add your user-agent to the Headers variable in the procedure PostSomeData, the same way as I added the Referer and Content-Type. The rest of the headers should be autogenerated by the browser.
    I really meant it. You need the content type to be application/x-www-form-urlencoded because you're POSTing the data to some place (you changed it to text/xml). Also, don't set the Content-length:, Accept: or the Host: at all -- these are automatically set by the Browser object. If anything, all you need to do is set the user-agent (and even that is not necessary unless the other end checks the user-agent).

    >> I wasn't sure where (if anywhere) I should put the POST /secure/WebDirectoryDownloadFromClient HTTP/1.0 statement either.
    Put that as the first parameter of Navigate:
    WebBrowser1.Navigate("http://www.somesite.com/secure/WebDirectoryDownloadFromClient", ....);

    The HTTP/1.0 part will be put in automatically by the Browser object.
    Last edited by Scorpions4ever; August 21st, 2004 at 11:57 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    6
    Rep Power
    0
    Thanks again for your help...

    I changed to content type to text/xml because that is how their documentation had it for this example. I tried both ways and received the same response.

    I did remove the other Header info and started to receive an XML generated FATAL error message from their server.

    I worked on it for another 3 hours and finally had enough. This morning it took 5 minutes to find the other problem:

    In their documentation, they have <DirectoryRequest"> instead of <DirectoryRequest>

    Ok, so now I am receiving an xml formatted response, what is the best way to work with this data from the TWebBrowser component:

    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    - <DirectoryResponse>
    - <IdentificationHeader>
    <TradingPartnerId>222222222</TradingPartnerId>
    <UserId>xyzuser</UserId>
    </IdentificationHeader>
    - <Transaction>
    <Function>DIRRESP</Function>
    <FilesReturnedCount>5</FilesReturnedCount>
    - <DirectoryList>
    <FileName>000000640467.040820040820.997</FileName>
    <FileCreationDate>08/20/2004</FileCreationDate>
    <FileType>997</FileType>
    <FileSize>000000000000251</FileSize>
    <LastDownloadDate>08/20/2004</LastDownloadDate>
    <LastDownloadUserId />
    </DirectoryList>
    - <DirectoryList>
    <FileName>000000640466.040820040820.997</FileName>
    <FileCreationDate>08/20/2004</FileCreationDate>
    <FileType>997</FileType>
    <FileSize>000000000000251</FileSize>
    <LastDownloadDate>08/20/2004</LastDownloadDate>
    <LastDownloadUserId />
    </DirectoryList>
    - <DirectoryList>
    <FileName>000000640464.040820040820.997</FileName>
    <FileCreationDate>08/20/2004</FileCreationDate>
    <FileType>997</FileType>
    <FileSize>000000000000251</FileSize>
    <LastDownloadDate>08/20/2004</LastDownloadDate>
    <LastDownloadUserId />
    </DirectoryList>
    - <DirectoryList>
    <FileName>000000640463.040820040820.997</FileName>
    <FileCreationDate>08/20/2004</FileCreationDate>
    <FileType>997</FileType>
    <FileSize>000000000000251</FileSize>
    <LastDownloadDate>08/20/2004</LastDownloadDate>
    <LastDownloadUserId />
    </DirectoryList>
    - <DirectoryList>
    <FileName>000000640462.040820040820.997</FileName>
    <FileCreationDate>08/20/2004</FileCreationDate>
    <FileType>997</FileType>
    <FileSize>000000000000251</FileSize>
    <LastDownloadDate>08/20/2004</LastDownloadDate>
    <LastDownloadUserId />
    </DirectoryList>
    </Transaction>
    </DirectoryResponse>
    Thanks.
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    You could always iterate through each element using the MSHTML interface, or you can grab the complete text and parse it manually yourself. To grab the text, do the following:

    1. In the uses section, add MSHTML_TLB to the list (see below for how to generate MSHTML_TLB).
    2. To grab the text from the WebBrowser:
    Code:
    var
    	Doc: IHTMLDocument2;
    	sText: string;
    begin
    	Doc := WebBrowser1.Document as IHTMLDocument2;
    	sText := Doc.body.outerText;
    end;
    Instructions for generating MSHTML_TLB
    ----------------------------------------
    See the following links:
    http://www.cryer.co.uk/brian/delphi/...MSHTML_TLB.htm
    http://bdn.borland.com/article/0,1410,26574,00.html
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    6
    Rep Power
    0
    Thanks once again... Because I am a novice at using this component... After I execute the procedure Tform1.PostSomeData below, if I run it again, it hangs. Do I need to clear it, reset it or anything?

    One more dilemma for now :

    Once I receive the DirectoryRequest XML response as outlined below. I will select a file from the list and then submit the following code: (Fname = 080000000008.040820040820.CSR)

    Code:
    procedure TForm1.DownloadFile(Fname: String);
    var
        strData : string;
        PostData: OleVariant;
        Headers: OleVariant;
        i : integer;
    
    begin
        strData :=
        '<?xml version="1.0" encoding="UTF-8"?>'+ #10#13 +
        '<DownloadRequest>'+ #10#13 +
        '<IdentificationHeader>'+ #10#13 +
        '<TradingPartnerId>222222222</TradingPartnerId>'+ #10#13 +
        '<UserId>xyzuser</UserId>'+ #10#13 +
        '<Password>xyzpassword</Password>'+ #10#13 +
        '</IdentificationHeader>'+ #10#13 +
        '<Transaction>'+ #10#13 +
        '<Function>DOWNLOAD</Function>'+ #10#13 +
        '<FileName>'+Fname+'</FileName>'+ #10#13 +
        '<FileFormat>TXT</FileFormat>'+ #10#13 +
        '</Transaction>'+ #10#13 +
        '</DownloadRequest>'+ #10#13; // Put your POST data here.'
        PostData := VarArrayCreate([0, Length(strData) - 1], varByte);
        for i := 1 to Length(strData) do PostData[i-1] := Ord(strData[i]);
        Headers := 'Content-Type: application/x-www-form-urlencoded' + #10#13;
        bReadPage := false;
        WebBrowser1.Navigate('https://www.somesite.com/secure/WebDownloadFromClient', EmptyParam, EmptyParam, postData, Headers);
        while not bReadPage do
            Application.ProcessMessages;
    end;
    When I run the above code, it just hangs. Should it automatically start downloading? If so how and how can I make it not bring up the standard save dialog box and just direct it to a location to save the file?

    Thanks
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    6
    Rep Power
    0

    Question


    Ok...The download procedure works...It ended up being an issue on their side.

    I still have two issues though:

    1) After I execute the procedure Tform1.PostSomeData below, if I run it again, it hangs. Do I need to clear it, reset it or anything? I also noticed that if I navigate to a standard URL (i.e. www.borland.com) and then execute the procedure again, it works fine.

    2) How can I take control of the download? For instance, I do not want it to prompt the user if they want to save or open the file. I want to be able to start d/l the file and directing it to the folder I choose. Is this possible?

    Thanks
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    1. Try calling the WebBrowser.Refresh method before calling it a second time. This sounds like a problem on the other end really.

    2. Try setting WebBrowser.Silent := true; in the code.
    http://msdn.microsoft.com/workshop/b...er2/silent.asp

    Alternatively, you can try using WebBrowser.ExecWB() with the correct cmd ID to save to disk. See the link above for documentation for ExecWB.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo