yxhxj2006

常用链接

统计

最新评论

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();
   }
  }
 }

posted on 2012-06-19 18:20 奋斗成就男人 阅读(3284) 评论(1)  编辑  收藏

评论

# re: java后台用post方式提交参数 2013-08-26 13:01 vfd

第三方  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: