java post 访问URL

URL url = new URL(" http://xxxxx.cn/smsSending.do ");
   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
   connection.setDoInput(true);
   connection.setDoOutput(true);
   PrintWriter out = new PrintWriter(connection.getOutputStream());
   //encode the message
   String userId = "userId="+URLEncoder.encode("xxxx","UTF-8");
   String pwd = "pwd="+URLEncoder.encode("1bbd886460827015e5d605ed44252251","UTF-8");
   String mobiles= "mobiles="+URLEncoder.encode("15221234154","UTF-8");
   String message = "message="+URLEncoder.encode("看到富士康电风扇电风扇电风扇地方","UTF-8");
   // send the encoded message
   out.print(userId+"&"+pwd+"&"+mobiles+"&"+message);
   out.flush();
   out.close();
   BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
   String line ;
   String result="";
   while((line = in.readLine())!=null){
    result+=line;
    System.out.println(result);
   }
   in.close();

你可能感兴趣的:(java,url,post)