Created
June 8, 2012 06:44
-
-
Save cy6erGn0m/2894017 to your computer and use it in GitHub Desktop.
Simplest HTTP request example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cg; | |
import java.io.InputStream; | |
import java.io.OutputStreamWriter; | |
import java.net.InetAddress; | |
import java.net.Socket; | |
/** | |
* @author Sergey Mashkov aka cy6erGn0m | |
* @since 08.06.12 | |
*/ | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
Socket s = new Socket(InetAddress.getByName("konn.ohooligans.ru"), 80); | |
try { | |
OutputStreamWriter w = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); | |
w.write("GET /bar HTTP/1.1\n"); | |
w.write("Host: konn.ohooligans.ru\n"); | |
w.write("Connection: close\n\n"); | |
w.flush(); | |
InputStream is = s.getInputStream(); | |
int rc; | |
byte[] buff = new byte[256]; | |
while ((rc = is.read(buff)) != -1) { | |
System.out.write(buff, 0, rc); | |
} | |
} finally { | |
s.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment