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).

1  
!7
2  
3  
p {
4  
  S url = "http://smartbot.botcompany.de/json/relations/is+a+standard+answer+to";
5  
  S hostname = hostNameFromURL(url), subURL = new URL(url).getFile();
6  
  int port = 80;
7  
  S method = "HEAD"; //"GET";
8  
  
9  
  Socket socket = new Socket(hostname, port);
10  
  BufferedReader reader = null;
11  
  PrintWriter writer = null;
12  
  
13  
  try {
14  
    writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "ASCII"));
15  
    httpPrintlnVerbose(writer, method + " " + subURL + " HTTP/1.1");
16  
    httpPrintlnVerbose(writer, "Host: " + hostname);
17  
    httpPrintlnVerbose(writer, "Accept: */*");
18  
    httpPrintlnVerbose(writer, "User-Agent: Java"); // Be honest.
19  
    httpPrintlnVerbose(writer, "X-No-Cookies: 1");
20  
    httpPrintlnVerbose(writer, ""); // Important, else the server will expect that there's more into the request.
21  
    writer.flush();
22  
    print("Sent request");
23  
    
24  
    reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ASCII"));
25  
    new L<S> lines;
26  
    for (String line; (line = reader.readLine()) != null;) {
27  
      if (line.isEmpty()) break; // Stop when headers are completed. We're not interested in all the HTML.
28  
      print("> " + line);
29  
      lines.add(line);
30  
    }
31  
    S s = first(lines);
32  
    int returnCode = parseFirstInt(substring(s, smartIndexOf(s, ' ')));
33  
    print("Got headers, return code: " + returnCode);
34  
 } finally {
35  
    if (reader != null) pcall { reader.close(); }
36  
    if (writer != null) pcall { writer.close(); }
37  
    if (socket != null) pcall { socket.close(); }
38  
  }
39  
}

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: 379 / 499
Version history: 9 change(s)
Referenced in: [show references]