public static Logger logger = Logger.getLogger(HttpClient.class);
public static JSONObject webservicePostMethods(String url,JSONObject paramsMap){
url=BACKEND_SERVICE_PROTOCOL
+ BACKEND_SERVICE_ADDR + ":" + BACKEND_SERVICE_PORT
+ BACKEND_APP+url;
logger.info("開始請求資源,URL:" + url);
int status = 0;
PostMethod pmethod = null;
HttpClient httpClient = null;
String resultJsonStr=null;
JSONObject jsonData = null;
httpClient= new HttpClient();
//httpClient= new HttpClient(new HttpClientParams(),new SimpleHttpConnectionManager(true));
pmethod = new PostMethod(url);
pmethod.getParams().setHttpElementCharset("utf-8");
pmethod.getParams().setContentCharset("utf-8");
pmethod.getParams().setUriCharset("utf-8");
//NameValuePair[] param ={ new NameValuePair("username", "vip")};
org.apache.commons.httpclient.NameValuePair[] param = new org.apache.commons.httpclient.NameValuePair[paramsMap.size()];
int i = 0;
Iterator iterator = paramsMap.keys();
while(iterator.hasNext()){
String key = (String) iterator.next();
param[i] = new org.apache.commons.httpclient.NameValuePair(key, paramsMap.getString(key));
i++;
}
pmethod.setRequestBody(param);
pmethod.getParams().setHttpElementCharset("utf-8");
pmethod.getParams().setContentCharset("utf-8");
pmethod.getParams().setUriCharset("utf-8");
try {
status = httpClient.executeMethod(pmethod);
if(status==200){
resultJsonStr=new String(pmethod.getResponseBodyAsString());
jsonData = JSONObject.fromObject(resultJsonStr);
logger.info("請求相應結果:" + resultJsonStr);
}
} catch (IOException e) {
e.printStackTrace();
}
return jsonData;
}