Skip to content

Instantly share code, notes, and snippets.

@yale8848
Created March 29, 2019 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yale8848/53fe1a7d0b55ed176b781f2bdb76f2b8 to your computer and use it in GitHub Desktop.
Save yale8848/53fe1a7d0b55ed176b781f2bdb76f2b8 to your computer and use it in GitHub Desktop.
Java socket request by local port
private static void test(){
try {
String path = "/";
String host = "publicobject.com";
int port = 80;
Socket socket = new Socket(InetAddress.getByName(host),port,InetAddress.getByName("172.16.1.251"),39981);
socket.setKeepAlive(false);
OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream(),"utf-8");
osw.write("GET " + path + " HTTP/1.0\r\n");
osw.write("Host: " + host + " \r\n");
osw.write("\r\n");
osw.flush();
socket.shutdownOutput();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(socket.getInputStream(), "utf-8"));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
osw.close();
bufferedReader.close();
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment