Created
December 16, 2011 11:09
-
-
Save xen/1485664 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Hashtable; | |
import java.util.Map; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
public class ApiTest { | |
private static final String SERVER_URL = "http://api.aplatform.ru"; | |
private static final String SECRET_KEY = "..."; | |
private static final String PRIVATE_KEY = "..."; | |
private static final String OID = "..."; | |
private static final String SID = "..."; | |
// private static final String PARTNER_URL = "http://mamba.ru/"; | |
public static void main(String[] args) { | |
testServerServer(); | |
testClientServer(); | |
} | |
private static void testServerServer() { | |
Map<String, String> param = new Hashtable<String, String>(); | |
param.put("app_id", "39"); | |
param.put("method", "anketa.getInfo"); | |
param.put("secure", "1"); | |
param.put("oids", OID); | |
param.put("sid", SID); | |
ArrayList<NameValuePair> params = Utils.prepareParam(param); | |
String sig = Utils.signServerServer(param, SECRET_KEY); | |
System.out.println("server-server sig: " + sig); | |
param.put("sig", sig); | |
params.add(new BasicNameValuePair("sig", sig)); | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpPost httpPost = new HttpPost(SERVER_URL); | |
String content = ""; | |
try { | |
httpPost.setEntity(new UrlEncodedFormEntity(params)); | |
HttpResponse response = httpClient.execute(httpPost); | |
content = Utils.readFromStream(response.getEntity().getContent()); | |
} catch (IllegalStateException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("testServerServer() content: " + content); | |
} | |
private static void testClientServer() { | |
Map<String, String> param = new Hashtable<String, String>(); | |
param.put("app_id", "39"); | |
param.put("method", "anketa.getInfo"); | |
param.put("secure", "0"); | |
param.put("oids", OID); | |
param.put("sid", SID); | |
ArrayList<NameValuePair> params = Utils.prepareParam(param); | |
String sig = Utils.signClientServer(param, OID, PRIVATE_KEY); | |
System.out.println("client-server sig: " + sig); | |
param.put("sig", sig); | |
params.add(new BasicNameValuePair("sig", sig)); | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpPost httpPost = new HttpPost(SERVER_URL); | |
String content = ""; | |
try { | |
httpPost.setEntity(new UrlEncodedFormEntity(params)); | |
HttpResponse response = httpClient.execute(httpPost); | |
content = Utils.readFromStream(response.getEntity().getContent()); | |
} catch (IllegalStateException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("testClientServer() content: " + content); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.Reader; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLEncoder; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.Map; | |
import java.util.SortedMap; | |
import java.util.TreeMap; | |
import java.util.Map.Entry; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.message.BasicNameValuePair; | |
public class Utils { | |
private static final String UTF8_CHARSET = "UTF-8"; | |
public static String signServerServer(Map<String, String> param, String secretKey) { | |
SortedMap<String, String> sortedParamMap = new TreeMap<String, String>(param); | |
StringBuilder buffer = new StringBuilder(); | |
buffer.append(canonicalize(sortedParamMap)).append(secretKey); | |
return MD5Hex(buffer.toString()); | |
} | |
public static String signClientServer(Map<String, String> param, String oid, String privateKey) { | |
SortedMap<String, String> sortedParamMap = new TreeMap<String, String>(param); | |
StringBuilder buffer = new StringBuilder(); | |
buffer.append(oid).append(canonicalize(sortedParamMap)).append(privateKey); | |
System.out.println("key: " + buffer.toString()); | |
return MD5Hex(buffer.toString()); | |
} | |
public static String getURL(Map<String, String> param) { | |
if (param.isEmpty()) { | |
return ""; | |
} | |
StringBuilder buffer = new StringBuilder(); | |
Iterator<Map.Entry<String, String>> iter = param.entrySet().iterator(); | |
while (iter.hasNext()) { | |
Map.Entry<String, String> kvpair = iter.next(); | |
buffer.append(encode(kvpair.getKey())); | |
buffer.append("="); | |
buffer.append(encode(kvpair.getValue())); | |
if (iter.hasNext()) { | |
buffer.append("&"); | |
} | |
} | |
String cannoical = buffer.toString(); | |
return cannoical; | |
} | |
public static String readFromStream(InputStream inputStream) throws IOException { | |
final char[] buffer = new char[0x10000]; | |
StringBuilder out = new StringBuilder(); | |
Reader in = new InputStreamReader(inputStream, UTF8_CHARSET); | |
int read; | |
do { | |
read = in.read(buffer, 0, buffer.length); | |
if (read > 0) { | |
out.append(buffer, 0, read); | |
} | |
} while (read >= 0); | |
return out.toString(); | |
} | |
public static ArrayList<NameValuePair> prepareParam(Map<String, String> param) { | |
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); | |
Iterator<Map.Entry<String, String>> iter = param.entrySet().iterator(); | |
while (iter.hasNext()) { | |
Entry<String, String> entry = iter.next(); | |
nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); | |
} | |
return nameValuePairs; | |
} | |
private static String canonicalize(SortedMap<String, String> sortedParamMap) { | |
if (sortedParamMap.isEmpty()) { | |
return ""; | |
} | |
StringBuffer buffer = new StringBuffer(); | |
Iterator<Map.Entry<String, String>> iter = sortedParamMap.entrySet().iterator(); | |
while (iter.hasNext()) { | |
Map.Entry<String, String> kvpair = iter.next(); | |
buffer.append(encode(kvpair.getKey())); | |
buffer.append("="); | |
buffer.append(encode(kvpair.getValue())); | |
} | |
String cannoical = buffer.toString(); | |
return cannoical; | |
} | |
private static String encode(String s) { | |
String out; | |
try { | |
out = URLEncoder.encode(s, UTF8_CHARSET); | |
} catch (UnsupportedEncodingException e) { | |
out = s; | |
} | |
return out; | |
} | |
private static String MD5Hex(String s) { | |
String result = null; | |
try { | |
MessageDigest md5 = MessageDigest.getInstance("MD5"); | |
byte[] digest = md5.digest(s.getBytes()); | |
result = toHex(digest); | |
} catch (NoSuchAlgorithmException e) { | |
// this won't happen, we know Java has MD5! | |
} | |
return result; | |
} | |
private static String toHex(byte[] a) { | |
StringBuilder sb = new StringBuilder(a.length * 2); | |
for (int i = 0; i < a.length; i++) { | |
sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16)); | |
sb.append(Character.forDigit(a[i] & 0x0f, 16)); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment