电竞比分网-中国电竞赛事及体育赛事平台

分享

客戶端post帶參數(shù)URL+XML,服務器端讀取

 昵稱20874412 2015-05-03
客戶端:

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


服務器端 

doPost


String username = request.getParameter("username"); 

用的都是Jdom哦
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,解決中文問題     
       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();
}

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多