1. Jean-Louis Bouty
  2. PowerBuilder
  3. Friday, 1 February 2019 10:24 AM UTC

Hello,

I have a script in Java to upload a file to a https site with cookie context. My question : is it possible to do the same instructions with the new object HttpClient in PB ? I have put in attachment the java script that works. Thanks for your help.

here is what i need equivalence in PB :


    private CloseableHttpClient httpClient = HttpClients.createDefault();

    private CookieStore cookieStore = new BasicCookieStore();//Will store the session cookie
    private HttpClientContext locContext = HttpClientContext.create();//Will contain the cookieStore

public int getConnection() throws ClientProtocolException, IOException {
        
        HttpPost httpPost = new HttpPost(getUrl().concat("/j_spring_security_check"));
       

    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    httpPost.setHeader("Connection", "keep-alive");
    List params = new ArrayList();
    params.add(new BasicNameValuePair("status", "A"));
    params.add(new BasicNameValuePair("j_username", "xxxxxxx"));
    params.add(new BasicNameValuePair("j_password", "yyyyyyy"));
    params.add(new BasicNameValuePair("enabled", "true"));
    httpPost.setEntity(new UrlEncodedFormEntity(params));

    locContext.setCookieStore(cookieStore);

    HttpResponse response = httpClient.execute(httpPost, locContext);

    return response.getStatusLine().getStatusCode();

public void uploadFile() throws ClientProtocolException, IOException {
    HttpPost uploadFile = new HttpPost(getUrl().concat("/protected/jwimportdeliveries/uploadDeliveries.do"));

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addTextBody("location", "C:/local/", ContentType.TEXT_PLAIN);

    // File f = new File("C:\\Local\\Mozaik_JLB_20190124.xlsx");
//        Lire_Ini file_ini = new Lire_Ini();
        String file_Moza = file_ini.get_file("FILE_MOZAIK");
        File f = new File(file_Moza);
    builder.addBinaryBody("File", new FileInputStream(f), ContentType.APPLICATION_OCTET_STREAM, f.getName());

    HttpEntity multipart = builder.build();
    uploadFile.setEntity(multipart);

    HttpResponse response = httpClient.execute(uploadFile, locContext);

    String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");

 

 

 

Attachments (1)
Mark Lee @Appeon Accepted Answer Pending Moderation
  1. Friday, 7 August 2020 06:11 AM UTC
  2. PowerBuilder
  3. # 1

Hi Jean-Louis,

Without a specific Web Server site, I wasn’t able to verify it with Postman or PB code.

Therefore, here I’m providing two PB code examples on how to send the cookies using HttpClient object for your reference. You can make some modifications according to your actual circumstances.

The first one:

HttpClient 	lhc_Client
String 		ls_Url, ls_id, ls_Method, ls_Body, ls_Respose, ls_txt, ls_Error,ls_header, ls_end
String 		ls_postdata, ls_file, ls_FileID, ls_FilePath
Blob 			lb_FileData, b
Long 			ll_rtn, ll_num, flen, loops, new_pos,i, bytes_read, ll_Code


lhc_Client = Create HttpClient

ls_FilePath = sle_filepath.Text

ll_Num= FileOpen(ls_FilePath,StreamMode!,Read!,LockRead!)
FileReadEx(ll_Num,lb_FileData)
FileClose(ll_Num)


ls_postdata = "file=" + String ( lb_FileData )
ls_Method = 'POST'
ls_Url = 'xxx.xxx.xxx'
lhc_Client.SetRequestHeader ( "Content-Type", "multipart/form-data; boundary=--------------------------359875084413580694217125" )
lhc_Client.SetRequestHeader ( "Connection", "keep-alive" )

ls_header = "----------------------------359875084413580694217125" + "~r~n" + & 
	'Content-Disposition: form-data; name="file"; filename="'+ ls_file +'"' + "~r~n" + &
	'Content-Type: image/jpeg' + "~r~n~r~n"
ls_End = "~r~n" + "----------------------------359875084413580694217125--"
lb_FileData = Blob (ls_header,EncodingUTF8!) + lb_FileData + Blob (ls_End,EncodingUTF8!)

lhc_Client.secureprotocol = 0
ll_rtn = lhc_Client.Sendrequest( ls_Method, ls_Url, lb_FileData )

If ll_rtn = 1 Then
	ls_Respose = lhc_Client.Getresponseheaders( )
	ll_rtn = lhc_Client.GetResponsebody( ls_Body)
	ll_Code = lhc_Client.GetResponseStatusCode()
	ls_txt = lhc_Client.GetResponseStatusText()
End If

If IsValid ( lhc_Client ) Then DesTroy ( lhc_Client )

 

The second one:

Integer 			li_rc
String 					ls_ReturnJson,	ls_StatusText
HttpClient 				lnv_HttpClient
CONSTANT	String		BOUNDARY = "----WebKitFormBoundary7MA4YWxkTrZu0gW"

lnv_HttpClient = Create HttpClient

String  ls_data

lnv_HttpClient.SetRequestHeader("Cookie", "RLTOK=c285522163dcb60ea8b1c56c83a93a0e%241%2403827f7ac7f737f29294e7c28d0e2aa9; SERVERUSED=www4")
lnv_HttpClient.SetRequestHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY)

ls_data ='--'+ BOUNDARY + '~r~nContent-Disposition: form-data; name="username"~r~n~r~n' &
	 + 'xxx' + '~r~n--' + BOUNDARY + '~r~nContent-Disposition: form-data; name="password"' &
	 + "~r~n~r~n" + 'xxx' + "~r~n--" + BOUNDARY+ "--"

 
// Sends request using POST method (to add the string data to the body )
li_rc = lnv_HttpClient.SendRequest("POST", "https://xxx.xxx.com/api/", ls_data)

// Obtains the response data
if li_rc = 1 and lnv_HttpClient.GetResponseStatusCode() = 200 then
	ls_StatusText = lnv_HttpClient.GetResponseStatusText()
	 lnv_HttpClient.GetResponseBody(ls_ReturnJson)
	 messagebox("GetResponseBody", ls_ReturnJson)
end if

destroy lnv_HttpClient

 

Regards,

 

Comment
  1. Mark Lee @Appeon
  2. Saturday, 24 October 2020 05:23 AM UTC
Hi Chris,

Thanks for sharing your experience with us.
  1. Helpful
  1. Miguel Leeuwe
  2. Wednesday, 28 July 2021 00:37 AM UTC
Hi Chris, this is a veeeery late comment, but just to point out: you can also use FileReadEx() to read "chunks" of data. The advantage it has, is that you can read BIG chunks, but small enough to get into trouble with the PBVM app storage. (Let's say 100MB chunks should be fine).
  1. Helpful
  1. Miguel Leeuwe
  2. Wednesday, 28 July 2021 00:38 AM UTC
correction: "small enough to NOT get into trouble..."
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Thursday, 6 August 2020 20:16 PM UTC
  2. PowerBuilder
  3. # 2

Hi Jean-Louis;

   Did you ever get this work in PB?

Regards ... Chris

Comment
  1. Jean-Louis Bouty
  2. Wednesday, 21 October 2020 08:23 AM UTC
Hi Chris,



I have not yet try the solution, I will do it.



Regards Jean-Louis
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.