Socket 传输

来源:互联网 发布:程序员转行做什么好 编辑:程序博客网 时间:2024/05/01 10:36

 public Object sendMessage(String content, String url) throws IOException
 {
  Object rsObj = null;
  long start = System.currentTimeMillis();
  BufferedReader r = null;
  String rs = "";
  try {
   URL u = new URL(url);
   URLConnection uc = u.openConnection();
   uc.setRequestProperty("Connection", "close");
   uc.setConnectTimeout(timeOut);
   uc.setDoOutput(true);
   uc.getOutputStream().write(content.getBytes("utf-8"));
   r = new BufferedReader(new InputStreamReader(uc.getInputStream(),
     "utf-8"));
   String line;
   StringBuffer buf = new StringBuffer();
   int i = 0;
   while ((line = r.readLine()) != null) {
    buf.append(line);
   }
   rs = buf.toString();
   rsObj = this.getResult(rs);
  } catch (IOException e) {
   // TODO: handle exception
   String errMsg = "接口异常==sendMessage("+content+","+url+")";
   log.error(errMsg);
   throw e;
  } finally {
   try {
    if (r != null)
     r.close();
   } catch (IOException ex) {
    // ignore
    throw ex;
   }
  }
  log.debug("remote time is : " + (System.currentTimeMillis() - start));
  return rsObj;
 }

原创粉丝点击