import java.io.*;
import java.net.*;
public class Post
{
 /**
  * @brief 用于向服务器 POST 数据完成登陆等操作
  *
  * @param server 指定服务器地址
  * @param method 指定此次的连接方法
  * @param config 参数信息
  */
 public static String connect(String server, String method, String config)
  throws Exception
 {
  // totalLine 用于返回最后发送数据后的结果
  String totalLine = new String();
  try {
   // url 是用于指定发送的地址
   // server 是服务器地址
   // method 是指定的模式(登陆、登出、强制登出、查询)
   URL url = new URL(server + method);
   // 打开链接 使用 POST 方法
   URLConnection conn = url.openConnection();
   conn.setDoOutput(true);
   conn.setDoInput(true);
   // 指定发送数据流
   OutputStreamWriter res =
    new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
   // 指定输出数据 config 内容参考文档
   res.write(config);
   // 输出数据 完成后关闭
   res.flush();
   res.close();
   // 读取返回的输入数据流 并用 BufferedRead 读入
   BufferedReader bReader =
    new BufferedReader(
     new InputStreamReader(conn.getInputStream()));
   // 单行读取直到为空
   String currentLine = new String();
   while ( (currentLine = bReader.readLine()) != null )
    totalLine += currentLine;
  }
  catch (Exception expt) {
   // 打印出异常原因
   //expt.printStackTrace();
   // 通常情况下都是本机联网失败 不在校园网内
   // 设置连接错误的信息 用于调用方处理
   totalLine = "connect error";
  }
  finally {
   // 返回此次 POST 的结果
   return totalLine;
  }
 }
 // 以下为测试代码
 public static void main(String[] args) throws Exception
 {
  final String server = "http://10.0.0.55/cgi-bin/";
  String method = "do_login";
  String mdpasswd = MD5Passwd.createMD5Passwd("deepblue04");
  String config = "username=ideepblue&password=" + mdpasswd
                        + "&drop=0&type=1&n=100";
  System.out.println(
   Post.connect(server,method,config));
  method = "keeplive";
  String uid = "630,9306985560";
                System.out.println(uid.matches(".+,*"));
  config = "uid=" + uid;