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

39
LINES

< > BotCompany Repo | #1009686 // Do my own HTTP requests Spike [OK]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1475L/11K/37K).

!7

p {
  S url = "http://smartbot.botcompany.de/json/relations/is+a+standard+answer+to";
  S hostname = hostNameFromURL(url), subURL = new URL(url).getFile();
  int port = 80;
  S method = "HEAD"; //"GET";
  
  Socket socket = new Socket(hostname, port);
  BufferedReader reader = null;
  PrintWriter writer = null;
  
  try {
    writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "ASCII"));
    httpPrintlnVerbose(writer, method + " " + subURL + " HTTP/1.1");
    httpPrintlnVerbose(writer, "Host: " + hostname);
    httpPrintlnVerbose(writer, "Accept: */*");
    httpPrintlnVerbose(writer, "User-Agent: Java"); // Be honest.
    httpPrintlnVerbose(writer, "X-No-Cookies: 1");
    httpPrintlnVerbose(writer, ""); // Important, else the server will expect that there's more into the request.
    writer.flush();
    print("Sent request");
    
    reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ASCII"));
    new L<S> lines;
    for (String line; (line = reader.readLine()) != null;) {
      if (line.isEmpty()) break; // Stop when headers are completed. We're not interested in all the HTML.
      print("> " + line);
      lines.add(line);
    }
    S s = first(lines);
    int returnCode = parseFirstInt(substring(s, smartIndexOf(s, ' ')));
    print("Got headers, return code: " + returnCode);
 } finally {
    if (reader != null) pcall { reader.close(); }
    if (writer != null) pcall { writer.close(); }
    if (socket != null) pcall { socket.close(); }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1009686
Snippet name: Do my own HTTP requests Spike [OK]
Eternal ID of this version: #1009686/10
Text MD5: 7a13cafe965a7101cbd0f4cca513f097
Transpilation MD5: 8ca9a0c36f3b670293190553f96e191e
Author: stefan
Category: javax / http
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-02 16:01:04
Source code size: 1558 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 445 / 580
Version history: 9 change(s)
Referenced in: [show references]