对于java用发送http请求,请求内容为xml格式 (二)

2014-11-24 10:31:16 · 作者: · 浏览: 1
InputStream(myPost.getResponseBodyAsStream());
byte[] bytes = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int count = 0;
while((count = bis.read(bytes))!= -1){
bos.write(bytes, 0, count);
}
byte[] strByte = bos.toByteArray();
responseString = new String(strByte,0,strByte.length,"utf-8");
bos.close();
bis.close();
}
}catch (Exception e) {
e.printStackTrace();
}
myPost.releaseConnection();
return responseString;
}

/**
* 用传统的URI类进行请求
* @param urlStr
*/
public void testPost(String urlStr) {
try {
URL url = new URL(urlStr);
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setRequestProperty("Pragma:", "no-cache");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "text/xml");

OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
String xmlInfo = getXmlInfo();
System.out.println("urlStr=" + urlStr);
// System.out.println("xmlInfo=" + xmlInfo);
out.write(new String(xmlInfo.getBytes("UTF-8")));
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(con
.getInputStream()));
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}

private String getXmlInfo() {
StringBuilder sb = new StringBuilder();
sb.append("< xml version='1.0' encoding='UTF-8' >");
sb.append("");
sb.append("
");
sb.append(" readMeetingStatus");
sb.append(" meeting");
sb.append(" xml");
sb.append(" admin");
sb.append(" admin");
sb.append(" box");
sb.append("
");
sb.append(" ");
sb.append(" 43283344");
sb.append(" ");
sb.append("
");

return sb.toString();