java实现http post发送

 public static String sendPostMessage(String urlString, String xmlString,
            String charset)
    {
        BufferedReader br = null;
        OutputStream os = null;
        StringBuffer sb = new StringBuffer();
        try
        {
            URL url = new URL(urlString);
            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            huc.setDoOutput(true);
            huc.setRequestMethod("POST");
            huc.setRequestProperty("Content-Type", "text/html; charset="+charset);
          
           
            //输出流,向服务端发送数据
            os = huc.getOutputStream();
            os.write(xmlString.getBytes());
           
            br = new BufferedReader(new InputStreamReader(huc.getInputStream(),
                    charset));
            String s = null;
            while ((s = br.readLine()) != null)
            {
                sb.append(s).append("\n");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        finally
        {
            if (os != null)
            {
                try
                {
                    os.close();
                }
                catch (IOException e)
                {
                   
                }
            }
            if (br != null)
            {
                try
                {
                    br.close();
                }
                catch (IOException e)
                {
                }
            }
        }
        return sb.toString();
    }

猜你喜欢

转载自starting.iteye.com/blog/1756170