java后台用post方式提交参数

java后台用post方式提交参数

 public static void main(String[] args) {
  URL url = null;
  HttpURLConnection httpurlconnection = null;
  try {
   url = new URL("http://www.ip138.com/ips.asp");
   httpurlconnection = (HttpURLConnection) url.openConnection();
   httpurlconnection.setDoInput(true);
   httpurlconnection.setDoOutput(true);

   httpurlconnection.setRequestMethod("POST");
   httpurlconnection.setRequestProperty("Content-Type",
     "application/x-www-form-urlencoded");

   String username = "ip=192.168.0.1";
   httpurlconnection.getOutputStream().write(username.getBytes());

   httpurlconnection.getOutputStream().flush();
   httpurlconnection.getOutputStream().close();
   int code = httpurlconnection.getResponseCode();
   System.out.println("code    " + code);

   if (code == 200) {

    String cookie = httpurlconnection.getHeaderField("Set-Cookie ");
    System.out.println(cookie);
    // httpurlconnection.setRequestProperty( "Cookie", cookie);

    DataInputStream in = new DataInputStream(httpurlconnection
      .getInputStream());
    int len = in.available();
    byte[] by = new byte[len];
    in.readFully(by);
    String rev = new String(by);
    System.out.println(rev);
    in.close();
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (httpurlconnection != null) {
    httpurlconnection.disconnect();
   }
  }
 }

你可能感兴趣的:(java后台用post方式提交参数)