Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

50
LINES

< > BotCompany Repo | #4000000 // ubaTaeCJ

User-supplied dialog

1  
static new ThreadLocal<Bool> doPost_silently;
2  
static new ThreadLocal<Long> doPost_timeout;
3  
static new ThreadLocal<SS> doPost_extraHeaders;
4  
5  
sS doPost(S url, Map urlParameters) {
6  
  ret doPost(urlParameters, url);
7  
}
8  
9  
sS doPost(Map urlParameters, S url) {
10  
  ret doPost(makePostData(urlParameters), url);
11  
}
12  
13  
static String doPost(S urlParameters, String url) ctex {
14  
  URL _url = new URL(url);
15  
  ping();
16  
  return doPost(urlParameters, _url.openConnection(), _url);
17  
}
18  
19  
static String doPost(S urlParameters, URLConnection conn, URL url) ctex {
20  
  bool silently = isTrue(optParam(doPost_silently));
21  
  Long timeout = optParam(doPost_timeout);
22  
  SS extraHeaders = optPar(doPost_extraHeaders);
23  
  setHeaders(conn);
24  
  for (S key : keys(extraHeaders)) {
25  
    ifdef doPost_debug
26  
      print("doPost sending header: " + key + " = " + extraHeaders.get(key));
27  
    endifdef
28  
    conn.setRequestProperty(key, extraHeaders.get(key));
29  
  }
30  
31  
  int l = lUtf8(urlParameters);
32  
  if (!silently)
33  
    print("Sending POST request: " + hideCredentials(url) + " (" + l + " bytes)");
34  
      
35  
  // connect and do POST
36  
  if (timeout != null) setURLConnectionTimeouts(conn, timeout);
37  
  cast conn to HttpURLConnection;
38  
  conn.setRequestMethod("POST");
39  
  conn.setDoOutput(true);
40  
  conn.setFixedLengthStreamingMode(l);
41  
  //conn.setRequestProperty("Content-Length", str(l));
42  
43  
  OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
44  
  writer.write(urlParameters);
45  
  writer.flush();
46  
47  
  String contents = loadPage_utf8(conn, url, false);
48  
  writer.close();
49  
  return contents;
50  
}

Author comment

1

download  show line numbers   

Travelled to 1 computer(s): mqqgnosmbjvj

No comments. add comment

Snippet ID: #4000000
Snippet name: ubaTaeCJ
Eternal ID of this version: #4000000/1
Text MD5: 6bee11333ec1f3160e8728404bb46d30
Author: ubataecj
Category: 1
Type: User-supplied dialog
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2024-04-06 17:29:01
Source code size: 1607 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 19 / 6
Referenced in: [show references]