/**
* socket套接字方式实现get请求模拟
* @param url
* @param param
* @return
*/
public static String getSocket(String url, String param) {
Socket socket = null;
BufferedWriter writer = null;
BufferedReader reader = null;
String result = "";
try {
//创建一个socket对象,用目标主机的IP和端口
socket = new Socket(IP,Integer.valueOf(PORT));
OutputStream output = socket.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(output));
InputStream input = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(input));
//===========请求参数===========
writer.write("GET /Sub"+param+" HTTP/1.0");
writer.write("\r\n");
writer.write("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
writer.write("\r\n");
writer.write("Host: "+HOST);
writer.write("\r\n");
writer.write("Proxy-Connection: Keep-Alive");
writer.write("\r\n");
writer.write("\r\n");
writer.flush();
//===========响应内容===========
if(input.available()>0){
String str = null;
while((str=reader.readLine())!=null){
System.out.println(str);
result += str;
}
System.out.println("发送成功");
}else if(input.available() == 0){
System.out.println("服务器端连接已关闭");
}
writer.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
https://www.cnblogs.com/embedded-linux/p/6261894.html