1. 代码部分参考
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
public class Main {
static String post="POST http://172.20.13.100:804/include/auth_action.php HTTP/1.1\n" +
"Host: 172.20.13.100:804\n" +
"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0\n" +
"Accept: */*\n" +
"Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3\n" +
"Accept-Encoding: gzip, deflate\n" +
"Referer: http://172.20.13.100:804/srun_portal_pc.php?ac_id=18&\n" +
"Content-Type: application/x-www-form-urlencoded\n" +
"Content-Length: 99\n" +
"Cookie: login=bQ0qQMxJvKa38joL2%252FjcHkC%252BmsG82yAvS0XGTkaUAqITzf1C1XjH0LBH3DvAVoqeDXiIyWwHluykns%252FbYtUUak1GPGqqZWFUnrgNFh3G6YRHOC3YK%252FNVgmMajbXNeOR0D%252B4olYy7aZwyNOm25ziIo1d%252BXXOAIuTVMqcPNt6JKFMANvY1f2yLD%252F7RXZuPjQVkeFeCU%252Fsbv0kNiKY%253D; login=bQ0qQMxJvKa38joL2%252FjcHkC%252BmsG82yAvS0XGTkaUAqITzf1C1XjH0LBH3DvAVoqeDXiIyWwHluykns%252FbYtUUak1GPGqqZWFUnrgNFh3G6YRHOC3YK%252FNVgmMajbXNeOR0D%252B4olYy7aZwyNOm25ziIo1d%252BXXOAIuTVMqcPNt6JKFMANvY1f2yLD%252F7RXZuPjQVkeFeCU%252Fsbv0kNiKY%253D\n" +
"Connection: keep-alive\n" +
"\n" +
"action=login&username=10111957@free&password={B}MjM3MjQ5&ac_id=18&user_ip=&nas_ip=&user_mac=&ajax=1";
public static void main(String[] args) {
try{
SocketChannel socketChannel=SocketChannel.open();
socketChannel.connect(new InetSocketAddress("172.20.3.90",80));
socketChannel.write(ByteBuffer.wrap(post.getBytes()));
ByteBuffer buffer=ByteBuffer.allocate(1024);
while(socketChannel.read(buffer)>0){
buffer.flip();
String content= Charset.forName("utf-8").decode(buffer).toString();
System.out.println(content);
buffer.clear();
}
socketChannel.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
2. 要获得上述代码中post字符串,首先在windows电脑上安装一个fiddler,真的是非常好用。然后点击tools->options->connections,图中端口号可以自己设置,默认为8888,Allow remote computers to connect勾选上,点击确定就行了。
3. 在linux系统中,我使用的是centos7,点击网络设置,代理设置,在HTTP代理栏目里,IP为安装了fiddler的windows电脑的IP,可以在windows电脑上使用ipconfig查看,端口号就填写fiddler中设置的那个。
4. 在linux中打开浏览器,我使用的是firefox,然后输入账号密码,点击登录,在fiddler中便可以看到相应的请求信息,
左边为请求的URI,右边点击RAW,便可以得到我们第一步中的post字符串,即将此处框框里的所有内容都复制到代码中的post字符串就行了,然后将该java文件编译成class文件,再将该class文件传输到linux服务器中的某个目录下。
5. 假如该class文件名为Main.class,使用
java Main
便可以成功联网了!!!