Created
March 29, 2019 02:41
-
-
Save yale8848/53fe1a7d0b55ed176b781f2bdb76f2b8 to your computer and use it in GitHub Desktop.
Java socket request by local port
This file contains hidden or 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
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