B4J Question HTTP Post [SOLVED]

moore_it

Well-Known Member
Licensed User
Longtime User
Hi all,

how i do for posting a file on https server ?

I try with Httpjob but not work this is the errormessage.

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

this is my code ...

B4X:
mHTTP.Initialize("HTTP", Me)
        mHTTP.Username = data(4)
        mHTTP.Password = data(5)
        mHTTP.PostString(data(3),msg)

where: data(4) = username, data(5) = password, data(3) = server address, msg = string data
 
Last edited:

aeric

Expert
Licensed User
Longtime User
I don't have PLD sample file. Maybe you can try with yours.

1715082794876.png
 

Attachments

  • PostMultiPart3.zip
    2.4 KB · Views: 8
Upvote 0

moore_it

Well-Known Member
Licensed User
Longtime User
Yes now work but have app version number error

1715086418720.png


reading the post of curl blog the user has the same problem !

There is not solution whit b4j
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Yes now work but have app version number error

View attachment 153489

reading the post of curl blog the user has the same problem !

There is not solution whit b4j
If you see my screen above, it is no longer reporting the error of AppVersion if you upload a file with the correct content.
I tried with Postman, it is the same. So I conclude it is not problem in B4J. I expect you will get the same issue if you use other programming language. To solve this issue, use the correct file, userid and password. Again, as I said, this service is already more than 20 years and no longer working. Unless I am wrong.
 
Upvote 0

moore_it

Well-Known Member
Licensed User
Longtime User
this is the return with the old program

--BOUNDARY
Content-type: text/html
Content-length: 138

<HTML>
<HEAD>
<TITLE>UPS Internet Software</TITLE>
</HEAD>
<BODY>
<P>UPS Internet Software, Copyright UPS 1998</P>
</BODY>
</HTML>

--BOUNDARY
Content-type: application/x-ups-psmpld
Content-length: 104

UPSOnLine%1.0%6930%5481PLD File submitted with above errors. Correct for next upload.

--BOUNDARY
Content-type: application/x-ups-pld
Content-length: 2688

000300002675200001920 KDGPLRS1 United Parcel Service 05/07/24 Host Access 10:33 AM PLD Error Report PAGE 1 ------------------------------------------------------------------------------- Err Error Code/ Message/ Tracking Seg Field Field Number ID Name Contents ------------------------------------------------------------------------------- 6246 PhoneNumber format is invalid. 1Z7V281X6860075618 *CA18 PhoneNumber 0821/84396 6246 PhoneNumber format is invalid. 1Z7V281X6860075609 *CA18 PhoneNumber 05684/930400 6246 PhoneNumber format is invalid. 1Z7V281X6860075556 *CA18 PhoneNumber 05531/7340 6246 PhoneNumber format is invalid. 1Z7V281X6860075510 *CA18 PhoneNumber 02632/42263 5481 PLD File submitted with above errors. Correct for next upload. 300000640 KDGPLCR1 United Parcel Service 05/07/24 Page:1 PLD Control Report 10:33 AM ------------------------------------------------------------------------------- Pickup Date:20240419 Sequence#:000005970579915 #of Segments:000000000000000222 Shipper#:7V281X Book/Page#:5970579915 Shipments:000032 Packages:0000000043 10000008800015481PLD File submitted with above errors. Correct for next upload.

--BOUNDARY--
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I need to use another programming language.
No. It's just that the UPS request does not fit a file based PostMultipart very well. I've looked at the documentation again, and both the Java and Visual Basic samples look horrendous. The Perl example is actually quite clear and can almost be ripped straight to B4X. Also, the sample text file provided above was totally stripped of all the unnecessary stuff (which probably interfered with your progress above) and I'm attaching the stripped-down version. I had to strip down the sample file I extracted from the documentation in post #11. With the code below, I get an invalid password response.

B4X:
Sub UploadFile
    Try
        'This is pretty much a straight rip off from the Perl example given in the docs
        Dim mCRLF As String = $"${Chr(13)}${Chr(10)}"$
        ' PLD TEST URL
        Dim upsURL As String = "https://www.pld-certify.ups.com/hapld/tos/kdwhapltos"
       
        ' open and readl the pld0200 file
        Dim pldFileContent As String = File.ReadString(File.DirAssets, "sample2.txt")
       
         ' create message part 1
        Dim msg_part1 As String ' = "AppVersion=1.0&AcceptUPSLicenseAgreement=YES&ResponseType=application/x-ups-pld&VersionNumber=V4R1&UserId=PLDDSTEST&Password=PLDDSTEST"
        'Java code sample mentions URL encoding these items
        msg_part1 = UrlEncodeMap(CreateMap("AppVersion":"1.0","AcceptUPSLicenseAgreement":"YES","ResponseType":"application/x-ups-pld","VersionNumber":"V4R1","UserId":"PLDDSTEST","Password":"PLDDSTEST"))
        Dim msg_part1_header1 As String = "Content-type: application/x-www-form-urlencoded"
        Dim msg_part1_header2 As String = $"Content-length: ${msg_part1.Length}"$
        msg_part1 = $"${msg_part1_header1}${mCRLF}${msg_part1_header2}${mCRLF}${mCRLF}${msg_part1}"$
       
        ' create message part 2
        Dim msg_part2 As String = pldFileContent
        Dim msg_part2_header1 As String = "Content-type: application/x-ups-binary"
        Dim msg_part2_header2 As String = $"Content-length: ${msg_part2.Length}"$

        msg_part2 = $"${msg_part2_header1}${mCRLF}${msg_part2_header2}${mCRLF}${mCRLF}${msg_part2}"$
       
        ' create the complete request message
        Dim PLDmsg As String = $"--BOUNDARY${mCRLF}${msg_part1}${mCRLF}${mCRLF}--BOUNDARY${mCRLF}${msg_part2}${mCRLF}${mCRLF}--BOUNDARY--"$
        Log(PLDmsg.Length)
        Dim job As HttpJob
        job.Initialize("", Me)
        'job.PostString("https://posttestserver.dev/p/0v2ofpzj1gqfm08m/post", PLDmsg)
        job.PostString(upsURL, PLDmsg)
        job.GetRequest.SetContentType("multipart/mixed; boundary=BOUNDARY")
        Log("Uploading file...")
        Wait For (job) JobDone(job As HttpJob)
        If job.Success Then
            Dim input As InputStream = job.GetInputStream
            Log($"Input bytes: ${input.BytesAvailable}"$)
            Log("Success: " & job.GetString)
        Else
            Log("Failed: " & job.ErrorMessage)
        End If
    Catch
        Log("Error: " & LastException.Message)
    End Try
    job.Release
End Sub

'https://www.b4x.com/android/forum/threads/b4x-urlencodemap.118626/
' UrlEncodeMap(input As Map) As String
' Converts a Map's keys/values to a URL encoded parameter string
' Following library is used
' B4J - jStringUtils
' B4A - StringUtils
' B4i - iStringUtils
Sub UrlEncodeMap(input As Map) As String
    Dim sb As StringBuilder
    Dim su As StringUtils
    sb.Initialize
    Dim first As Boolean = True
    For Each key In input.Keys
        If Not(first) Then
            sb.Append("&")
        Else
            first = False
        End If
        sb.Append($"${su.EncodeUrl(key, "UTF8")}=${su.EncodeUrl(input.Get(key), "UTF8")}"$)
    Next
    Return sb.ToString
End Sub
 

Attachments

  • sample2.txt
    719 bytes · Views: 8
Last edited:
Upvote 0

moore_it

Well-Known Member
Licensed User
Longtime User
yes yes yesssssssssssssssss great this work fine !!!!!!!!

1715094357592.png


Already processed ... this is true !

Thanks so much !!!
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Where do you get a sample text file?
I guess I have no clue. For some reason I thought I got it off a post here. :eek:
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Where do you get a sample text file?
Found it. It's in the PLD documentation under Request. The bottom of the doc contains the sample text file. I actually stripped down that file, not anything that was posted here.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
and not

CRLF
Depending on platform, the CRLF, may just be CR instead of CRLF. By coding it this way, I'm guaranteed CRLF no matter what platform it runs.
 
Upvote 0
Top