读书人

简略HTTP接口服务客户端DEMO(CQYHT)

发布时间: 2012-08-29 08:40:14 作者: rapoo

简单HTTP接口服务客户端DEMO(CQYHT)

?

Demo程序:

?

package org.acooly.onlycall.service;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.apache.commons.io.IOUtils;public class ServiceClientDemo {private static final String DEFAULT_ENCODING = "UTF-8";private static final String SERVICE_ENDPOINT = "http://域名/service";public static void main(String[] args) throws Exception {ServiceClientDemo demo = new ServiceClientDemo();demo.testCqyhtService();}public void testCqyhtService() {Map<String, String> parameters = new HashMap<String, String>();parameters.put("Action", "CALL");parameters.put("PhoneNo", "一号通号码");parameters.put("Credential", "XXXXXX");parameters.put("CallerNo", "主叫");parameters.put("CalledNos", "被叫");String queryString = getCanonicalizedQueryString(parameters, false);String requestUrl = SERVICE_ENDPOINT + "?" + queryString;System.out.println("Request: \n" + requestUrl);String response = httpSimpleRequest("GET", requestUrl, null);System.out.println("Response Body: \n" + response);}protected String httpSimpleRequest(String method, String requestUrl, Map<String, String> headers) {HttpURLConnection conn = null;InputStream in = null;String result = null;try {URL url = new URL(requestUrl);conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod(method);conn.setDoInput(true);conn.setDoOutput(true);if (headers != null) {for (Map.Entry<String, String> entry : headers.entrySet()) {conn.addRequestProperty(entry.getKey(), URLEncoder.encode(entry.getValue(), "UTF-8"));}}int status = conn.getResponseCode();System.out.println("Response status: " + status);in = conn.getErrorStream();if (in != null) {result = IOUtils.toString(in);}in = conn.getInputStream();if (in != null) {result = IOUtils.toString(in);}} catch (Exception e) {System.err.println(e.getMessage());} finally {IOUtils.closeQuietly(in);}return result;}protected String getCanonicalizedQueryString(Map<String, String> parameters, boolean encoding) {StringBuilder builder = new StringBuilder();Iterator<Map.Entry<String, String>> pairs = parameters.entrySet().iterator();while (pairs.hasNext()) {Map.Entry<String, String> pair = pairs.next();String key = pair.getKey();String value = pair.getValue();builder.append(encoding ? urlEncode(key, false) : key);builder.append("=");builder.append(encoding ? urlEncode(value, false) : value);if (pairs.hasNext()) {builder.append("&");}}return builder.toString();}protected String urlEncode(String value, boolean path) {try {String encoded = URLEncoder.encode(value, DEFAULT_ENCODING).replace("+", "%20").replace("*", "%2A").replace("%7E", "~");if (path) {encoded = encoded.replace("%2F", "/");}return encoded;} catch (UnsupportedEncodingException ex) {throw new RuntimeException(ex);}}}

?

?

运行结果:

?

?

Request: http://域名/service?Action=CALL&CalledNos=被叫&Credential=XXXXXX&CallerNo=主叫&PhoneNo=一号通号码Response status: 200Response Body: <?xml version="1.0" encoding="UTF-8"?><Result><Code>0001</Code><Message>一号通号码不合法或用户被禁用</Message></Result>
?

?

?

?

?

?

读书人网 >互联网

热点推荐