import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
public class HttpClientTest {
String data = "<root><header><type>fetch</type></header><content><program>test</program></content></root>";
String url = "http://localhost:8080/SgWebSer/AppStatus?username=cc&pwd=11";
public void postSend() {
HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod(url);
String info = null;
try {
RequestEntity entity = new StringRequestEntity(data, "text/xml",
"iso-8859-1");
post.setRequestEntity(entity);
httpclient.executeMethod(post);
int code = post.getStatusCode();
if (code == HttpStatus.SC_OK)
info = new String(post.getResponseBodyAsString());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
post.releaseConnection();
}
}
public static void main (String[] args)
{
HttpClientTest hct = new HttpClientTest();
hct.postSend();
}
}
SAXBuilder builder = new SAXBuilder();
InputSource is = new InputSource();
try {
is.setByteStream(request.getInputStream());
is.setEncoding("iso-8859-1");
Document document = builder.build(is);
document.getRootElement().getChild("header").getChild("type").getText();
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");// 設置xml文件的字符為UTF-8,解決中文問(wèn)題
XMLOutputter xmlout = new XMLOutputter(format);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
xmlout.output(document, bo);
String s = bo.toString();
System.out.println(bo.toString());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}