Developer Forum [ All Topics | New Topic ]

Thread: Intergrate HTTP Post file in Visual Basic 6

Hello, Is there anyone who figured out how to implement the HTTP Posting of inventory files in Visual Basic 6 code? Please help me. Thank you,

>> add a comment
tom
12/16/2009 05:01


5 Replies:

I don't have visual basic 6, so I can check this, but it should be something like this: Dim strPost As String Dim objHTTP, replyTXT As String Dim AuthenticationTicket As String AuthenticationTicket = "" Set objHTTP = CreateObject("Msxml2.ServerXMLHTTP") strPost = "https://technet.rapaport.com/HTTP/Authenticate.aspx" 'Get authentication ticket: objHTTP.Open "POST", strPost, False objHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") objHTTP.send("Username=" & strUser & "&Password=" & strPWD) replyTXT = objHTTP.responseText If objHTTP.Status = "200" Then 'success AuthenticationTicket = replyTXT Else 'authentication failed, you need to deal with this. AuthenticationTicket = "" End If 'POST data Dim PostURL = "http://technet.rapaport.com/HTTP/Upload/Upload.aspx?Method=string" PostURL = PostURL + "&ticket=" + AuthenticationTicket Dim StockData = "Shape, Size, Color, Clarity, comments" StockData = StockData + "Round, 1.00, E, VS1, " + vbCRLF StockData = StockData + "Round, 2.00, F, VS1, my comments" + vbCRLF StockData = StockData + "Pear, 1.00, G, VS1, it's sunny" + vbCRLF StockData = StockData + "Round, 1.00, E, VS1, ""quoted string""" + vbCRLF objHTTP.Open("POST", PostURL, False) objHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") objHTTP.send(StockData) replyTXT = objHTTP.responseText If you want, you can email me some working code, so I can add it to the code samples. it@diamonds.net Regards, Leo

>> add a comment
Leo Muller
12/17/2009 2:08:00 AM
Thank you very much Leo for posting this code. With a few changes, your code works. The only thing I'm having problem with now is uploading the file, since your code shows how to upload a LOT using a formatted string. If you think you can help me, I would really appreciate it. But if not, I want to say Thank YOU again, you've already helped me. Regards, Tom

>> add a comment
Tom.
12/17/2009 11:39:00 AM
I don't have any code at hand to post files themselves from VB6, but it may look something like this: objHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") objHTTP.setRequestHeader("Content-Disposition", "form-data;name=stock.csv;filename=c:\stock.csv") Another approach would be to use the first code, and read your text file, and add line by line to the text that you send in POST (StockData ) - just make sure to add linebreaks, so the CSV format will be good. Regards, Leo

>> add a comment
Leo Muller
12/20/2009 1:46:00 AM
Private Sub PostHTTP() On Error GoTo ErrHandler Dim strPost As String Dim objHTTP, replyTXT As String Dim AuthenticationTicket As String Dim sFileName As String, strUser as String, strPWD as string dlgExport.ShowOpen sFileName = dlgExport.FileName AuthenticationTicket = "" Set objHTTP = CreateObject("Msxml2.ServerXMLHTTP") strPost = "https://technet.rapaport.com/HTTP/Authenticate.aspx" strUser = "myUserName" strPWD = "myPassword" 'Get authentication ticket: objHTTP.Open "POST", strPost, False Call objHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") objHTTP.Send ("Username=" & strUser & "&Password=" & strPWD) replyTXT = objHTTP.responseText If objHTTP.Status = "200" Then 'success AuthenticationTicket = replyTXT Else 'authentication failed, you need to deal with this. AuthenticationTicket = "" End If 'POST data Dim PostURL As String PostURL = "http://technet.rapaport.com/HTTP/Upload/Upload.aspx?Method=file&ReplaceAll=true" '&FirstRowHeaders=true" '&LotListFormat=Rapnet&Filename=" & sFileName PostURL = PostURL + "&ticket=" + AuthenticationTicket Dim StockData As String StockData = dlgExport.FileTitle Call objHTTP.Open("POST", PostURL, False) Call objHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") Call objHTTP.setRequestHeader("Content-Disposition", "form-data;name=stock.csv;filename=" & sFileName) objHTTP.Send (vbNullString) replyTXT = objHTTP.responseText Debug.Print replyTXT Exit Sub ErrHandler: MsgBox Err.Number & ": " & Err.Description End Sub ====================================================== Above is the code that i'm trying to running with Visual Basic 6. The Authentication runs successfully and I get the AuthenticationTicket, but in the next step, when sendting the request of uploading the file, i get an error response from the server "Error: You did not provide a file to upload your stones.
Please provide us using HTTP Forms a CSV file containing the stones" As you can see in the code, I do provide the file. Please if anyone can help me, i would greately appreciate it. Thank you, Tom P.S. I want to say Thanks again to Leo, even though i can't get the code to work, you still helped to get a sense of how it's done in Visual Basic. Thank you very much!

>> add a comment
Tom
12/21/2009 11:51:00 AM
i get this error too.. did anyone get past this????

>> add a comment
eg252
1/25/2010 1:06:00 PM