[Java] java模拟http请求 HttpURLConnection发送POST请求 →→→→→进入此内容的聊天室

来自 , 2019-07-16, 写在 Java, 查看 105 次.
URL http://www.code666.cn/view/301ad0e3
  1. @Override
  2.         public void run() {
  3.                 // TODO Auto-generated method stub
  4.  
  5.                         String reqUrl = "http://xxxxxxxxxx.com";
  6.                         String postContent = "addsubmit=true";
  7.                        
  8.                         try {
  9.                                 url = new URL(reqUrl);
  10.                                 conn = (HttpURLConnection) url.openConnection();
  11.                                 conn.setRequestMethod("POST");
  12.                                 conn.setDoOutput(true);
  13.                                 conn.setDoInput(true);
  14.                                 conn.setUseCaches(false);
  15.                                 conn.setRequestProperty("Content-Type",
  16.                                                 "application/x-www-form-urlencoded");
  17.                                 conn.setRequestProperty("Cookie", cookie); // 注入cookie (String cookie)
  18.                                 OutputStreamWriter osw = new OutputStreamWriter(
  19.                                                 conn.getOutputStream(), "UTF-8");
  20.                                 osw.write(postContent.toString());
  21.                                 osw.flush();
  22.                                 osw.close();
  23.                         } catch (Exception e) {
  24.                                 // e.printStackTrace();
  25.  
  26.                                 Log.e("发送请求超时!");
  27.                         }
  28.  
  29.                         try {
  30.                                 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
  31.                                         // 读取返回内容
  32.                                         StringBuffer buffer = new StringBuffer();
  33.                                         try {
  34.                                                 BufferedReader br = new BufferedReader(
  35.                                                                 new InputStreamReader(conn.getInputStream(),
  36.                                                                                 "UTF-8"));
  37.                                                 String temp;
  38.                                                 while ((temp = br.readLine()) != null) {
  39.                                                         buffer.append(temp);
  40.                                                         buffer.append("\n");
  41.                                                 }
  42.                                         } catch (Exception e) {
  43.                                                 e.printStackTrace();
  44.  
  45.                                                 Log.e("读取结果失败");
  46.  
  47.                                         }
  48.  
  49.                                         Log.d(buffer.toString());
  50.  
  51.                                         int pos = buffer.toString().indexOf("id=\"message\"");  //查找返回页面关键词
  52.  
  53.                                         if (pos == -1) {
  54.                                                 pos = buffer.toString().indexOf("class=\"list_tip");
  55.                                         }
  56.  
  57.                                         if (pos == -1) {
  58.                                                 Log.e("查找结果关键字失败!");       
  59.                                         }
  60.  
  61.                                 } else {
  62.                                         Log.e("页面错误!");
  63.  
  64.                                 }
  65.                         } catch (IOException e) {
  66.                                 // TODO Auto-generated catch block
  67.                                 // e.printStackTrace();
  68.  
  69.                                 Log.e("读取结果超时!");
  70.  
  71.                         }
  72.  
  73.         }

回复 "java模拟http请求 HttpURLConnection发送POST请求"

这儿你可以回复上面这条便签

captcha