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

44
LINES

< > BotCompany Repo | #1000565 - doPost

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3717L/25K).

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

static S 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);
  ((HttpURLConnection) conn).setRequestMethod("POST");
  conn.setDoOutput(true);
  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;
}

download  show line numbers  debug dex   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz, whxojlpjdney

Comments [hide]

ID Author/Program Comment Date
752 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #1000565
Snippet name: doPost
Eternal ID of this version: #1000565/17
Text MD5: ab3d77472492ecf38c6b11a1dfba1876
Transpilation MD5: 0de72f225be1de0e97865682f6049873
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-12-07 13:32:13
Source code size: 1479 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 687 / 3327
Version history: 16 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1000565 & http://1000565.tinybrain.de