代码:
public class ItfTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
String out = testbiz();
System.out.println(out);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String testbiz() throws HttpException, IOException {
HttpClient httpClient = new HttpClient();
PostMethod post = new PostMethod( "http://localhost:8001/itf/OI_AddSaleByParameter");
// PostMethod post = new PostMethod( "https://rryx.sinovatech.com/itf/OI_QueryEShopOrder");
post.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=utf-8");
String key = "123321abc";
String channel="ch_shenxun";
Map<String, Object> req = new HashMap<>();
String instr ="{\"body\": \"{\\\"startTime\\\":\\\"2019-01-01 14:43:19\\\",\\\"endTime\\\":\\\"2019-01-12 15:14:42\\\",\\\"phone\\\":\\\"18309858042\\\"}\",\"head\": {} }";
/* String instr ="6351";
req.put("loginCode",instr);*/
Map<String, Object> header = new HashMap<>();
header.put("appId", channel);
header.put("tid", UUID.randomUUID().toString());
header.put("timestamp", System.currentTimeMillis());
header.put("ver", "1.0");
Map<String, Object> reMap = new HashMap<>();
reMap.put("header", header);
reMap.put("req", req);
req.put("saleName","123");
req.put("goodIds","ff8080815e562c4d015e6103956700b6");
req.put("orderValue","1");
req.put("createName","管理员");
System.out.println(JSONObject.toJSON(reMap));
String reqdata = JSONObject.toJSONString(reMap);
String sign = DigestUtils.md5Hex(reqdata + key);
NameValuePair[] param = { new NameValuePair("sign", sign),
new NameValuePair("reqdata", reqdata) };
post.setRequestBody(param);
int result = httpClient.executeMethod(post);
System.out.println("返回结果=" + result);
InputStream in = post.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
while (line != null) {
sb.append(line);
line = reader.readLine();
}
reader.close();
return sb.toString();
}
}
//以上代码传参测试,但是运行后报错
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:286)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at com.sinovatech.test.ItfTest.testbiz(ItfTest.java:95)
at com.sinovatech.test.ItfTest.main(ItfTest.java:52)
求大神指点