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();
}
java实现http post发送
最新推荐文章于 2024-04-29 16:26:06 发布