package com.jeecms.cms.webservices;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class HttpTest {
public static void main(String[] args) throws Exception {
reqHttpUrl();
}
public static void reqHttpUrl() throws JSONException{
DefaultHttpClient httpClient = new DefaultHttpClient();
String url="http://192.168.1.1";java
/**web
入參要求格式 出參apache
請求報文:json
{"body":{"MOBILENUM":"13444544444"},"head":{"sign":"04eba9c5e61022ed3706fd8e15953a4b3d7","appid":"2-0001","version":"2.0"}}app
響應報文:url
Response: {"head":{"rtnMsg":"本次請求成功!","rtnCode":"000000"},"body":false}3d
**/
HttpPost method=new HttpPost(url);
JSONObject jsonParam=new JSONObject();
JSONObject jsonParam1=new JSONObject();
jsonParam.put("MOBILENUM", "13444544444");
jsonParam1.put("sign", "04eba9c5e61022ed3706fd8e15953a4b3d7");
jsonParam1.put("appid", "2-0001");
jsonParam1.put("version", "2.0");
JSONObject jsonParam2=new JSONObject();
jsonParam2.put("body", jsonParam);
jsonParam2.put("head", jsonParam1);
System.out.println(jsonParam2.toString());
StringEntity entity = new StringEntity(jsonParam2.toString(),"utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
method.setEntity(entity);
try {
HttpResponse result = httpClient.execute(method);
String resData = EntityUtils.toString(result.getEntity());
JSONObject json=new JSONObject(resData);
JSONObject json1=new JSONObject(json.get("head").toString());
System.out.println(json1.get("rtnCode"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
utf-8