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

50
LINES

< > BotCompany Repo | #4000001 // ubaTaeCJ

User-supplied dialog

static new ThreadLocal<Bool> doPost_silently;
static new ThreadLocal<Long> doPost_timeout;
static new ThreadLocal<SS> doPost_extraHeaders;

sS doPost(S url, Map urlParameters) {
  ret doPost(urlParameters, url);
}

sS doPost(Map urlParameters, S url) {
  ret doPost(makePostData(urlParameters), url);
}

static String doPost(S urlParameters, String url) ctex {
  URL _url = new URL(url);
  ping();
  return doPost(urlParameters, _url.openConnection(), _url);
}

static String doPost(S urlParameters, URLConnection conn, URL url) ctex {
  bool silently = isTrue(optParam(doPost_silently));
  Long timeout = optParam(doPost_timeout);
  SS extraHeaders = optPar(doPost_extraHeaders);
  setHeaders(conn);
  for (S key : keys(extraHeaders)) {
    ifdef doPost_debug
      print("doPost sending header: " + key + " = " + extraHeaders.get(key));
    endifdef
    conn.setRequestProperty(key, extraHeaders.get(key));
  }

  int l = lUtf8(urlParameters);
  if (!silently)
    print("Sending POST request: " + hideCredentials(url) + " (" + l + " bytes)");
      
  // connect and do POST
  if (timeout != null) setURLConnectionTimeouts(conn, timeout);
  cast conn to HttpURLConnection;
  conn.setRequestMethod("POST");
  conn.setDoOutput(true);
  conn.setFixedLengthStreamingMode(l);
  //conn.setRequestProperty("Content-Length", str(l));

  OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  writer.write(urlParameters);
  writer.flush();

  String contents = loadPage_utf8(conn, url, false);
  writer.close();
  return contents;
}

Author comment

1

download  show line numbers   

Travelled to 1 computer(s): mqqgnosmbjvj

No comments. add comment

Snippet ID: #4000001
Snippet name: ubaTaeCJ
Eternal ID of this version: #4000001/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: 112 / 23
Referenced in: [show references]