`

HTTP类 调用远程接口添加json 参数,返回json参数

阅读更多
package res;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import net.sf.json.JSONObject;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
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;

@SuppressWarnings("deprecation")
public class HttpClientVisitTest {

static Properties prop = null;
static {
InputStream is = HttpClientVisitTest.class
.getResourceAsStream("//config//ip.properties");
prop = new Properties();
try {
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
}
}
private static final String HttpUrl = prop.getProperty("url");
private static final String HttpHost = prop.getProperty("ip");

public JSONObject testHttpClientVisit(String token, String Authorization) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(HttpUrl);
JSONObject msg = null;
int resultCode = 0;
try {
StringEntity s = new StringEntity(getMsg(token).toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
httpPost.setEntity(s);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
HttpResponse httpResponse = null;

try {
HttpHost httpTarget = new HttpHost(HttpHost, Integer.parseInt(prop
.getProperty("port")), "http");
System.out.println("#####已经发送请求服务器 :" + new Date() + ",$$$,"
+ httpTarget);
httpResponse = httpClient.execute(httpTarget, httpPost);
    resultCode = httpResponse.getStatusLine().getStatusCode();
if(resultCode == 500){
msg = new JSONObject();
JSONObject header = new JSONObject();
header.put("resultcode", "-1");
msg.put("header", header);
return msg;
}
    String ss=EntityUtils.toString(httpResponse.getEntity());
msg=JSONObject.fromObject(ss);
} catch (Exception e) {
System.out.println("error############# 远程异常  请求报错,请查看 :" + resultCode);
e.printStackTrace();
msg = new JSONObject();
JSONObject header = new JSONObject();
header.put("resultcode", "-1");
msg.put("header", header);
return msg;
} finally {
if (httpPost.isAborted()) {
httpPost.abort();
}
httpClient.getConnectionManager().shutdown();
}
return msg;
}

/**
* 返回请求参数
*
* @return
*/
public JSONObject getMsg(String token) {
JSONObject header = new JSONObject();
JSONObject body = new JSONObject();
JSONObject msg = new JSONObject();
header.put("version", "1.0");
header.put("msgid", "abcde");// 目前没用处
String p_pattern = "yyyyMMddHHmmssSSS";
SimpleDateFormat p_sdf = new SimpleDateFormat(p_pattern);
// 定义要转换的Date对象,我的例子中使用了当前时间
Calendar p_cal = Calendar.getInstance();
Date p_date = p_cal.getTime();
// 输出结果
System.out.println("====>" + p_sdf.format(p_date));
header.put("systemtime", p_sdf.format(p_date).toString());
header.put("sourceid", "");
header.put("appid", "5");
body.put("token", token);
msg.put("header", header);
msg.put("body", body);
return msg;
}

}


//下面是配置文件信息
url=http://192.168.107.221:9042/idmp/client/tokenValidate
ip=192.168.107.221
port=9042
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics