以下是我在测试中无法成功的安卓请求代码(在确保安卓权限没有问题的情况下)
Code: Select all
HttpURLConnection connection=null;
URL url = new URL("http://192.168.206.108/rfid_data");
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000);
//设置请求方式 GET / POST 一定要大小
//设置请求参数
connection.setRequestMethod("POST");
//添加Header
connection.setRequestProperty("Connection", "Keep-Alive");
//接收输入流
connection.setDoInput(true);
//传递参数时需要开启
connection.setDoOutput(true);
//Post方式不能缓存,需手动设置为false
connection.setUseCaches(false);
connection.connect();
// 传入字节流
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
String postContent = "hello 安卓!";
dos.write(postContent.getBytes());
dos.flush();
// 执行完dos.close()后,POST请求结束
dos.close();