---
title: "java实现http post发送"
author: "starting"
date: "2012-12-28T10:14:55Z"
tags: []
categories: ["研发管理"]
slug: "java-http-post"
draft: false
lastmod: "2012-12-28T10:14:55Z"
id: 1756170
url: "https://starting.iteye.com/blog/1756170"
---

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