public class TestHttp {
public void testRegist(){
try{
StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> ");
sb.append("<getData>");
sb.append("<recNum>2010</recNum>");
sb.append("</getvData>");
byte[] xmlbyte = sb.toString().getBytes("UTF-8");
URL url = new URL("http://xx:8180/xuxxx/xxxxxx");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(xmlbyte);
outStream.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb2 = new StringBuffer();
String lines = "";
while(null!=(lines = in.readLine()))
{
sb2.append(lines);
}
System.out.println(sb2.toString());
// in.read(b)
in.close();
outStream.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
/**
*
* @param urlStr 接口地址
* @param xmlInfo xml格式参数数据
* @return
*/
public static String sendMsgXml(String urlStr, String xmlInfo) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(urlStr);
URLConnection con = url.openConnection();
con.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(), "utf-8");
System.out.println("Exedata start\n" + xmlInfo + "\nExe end");
out.write(xmlInfo);
//out.write(new String(request.getBytes("ISO-8859-1")));
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
buffer.append(line);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buffer+"";
}