Java请求RapNet接口查询钻石价格

RapNet的账号开通在国内需要联系香港的办公室,具体信息:
Hong Kong & China
+852-2805-2620
hk@diamonds.net

获取钻石价格需要2个接口,一个获取钻石列表,另一个获取钻石的详细信息。

通过WebServer获取

这种方式除了上面的2个接口外还需要一个接口获取ticket,请求和处理返回值都相对比较麻烦,就放弃了。

获取JSON格式数据

每次请求携带用户、密码和查询参数,服务端返回json格式的字符串。

Java代码:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

public class RapNet {

private final static String URL_GET_DIAMONDS = "https://technet.rapaport.com/HTTP/JSON/RetailFeed/GetDiamonds.aspx";

private final static String URL_GET_SINGLE_DIAMOND = "https://technet.rapaport.com/HTTP/JSON/RetailFeed/GetSingleDiamond.aspx";

//默认每页查询50条
private final static int DEFAULT_PAGE_SIZE = 50;

public static String getDiamonds(String username, String password, int pageNumber) {
return getDiamonds(username, password, DEFAULT_PAGE_SIZE, pageNumber);
}

public static String getDiamonds(String username, String password, int pageSize, int pageNumber) {
Map<String, String> header = new HashMap<>();
header.put("username", username);
header.put("password", password);

Map<String, Object> body = new HashMap<>();
// 查询参数
body.put("search_type", "White");
body.put("shapes", Arrays.asList("round"));
body.put("labs", Arrays.asList("GIA"));

// 分页参数
body.put("page_size", String.valueOf(pageSize));
body.put("page_number", String.valueOf(pageNumber));
body.put("sort_by", "price");
body.put("sort_direction", "Asc");

Map<String, Object> request = new HashMap<>();
request.put("header", header);
request.put("body", body);

Map<String, Object> params = new HashMap<>();
params.put("request", request);

// 查询参数转为json格式
String str = JSON.toJSONString(params);

String rs = post(URL_GET_DIAMONDS, str);

return rs;
}

public static String getSingleDiamond(String username, String password, int diamondId) {

Map<String, String> header = new HashMap<>();
header.put("username", username);
header.put("password", password);

Map<String, Integer> body = new HashMap<>();
body.put("diamond_id", diamondId);

Map<String, Object> request = new HashMap<>();
request.put("header", header);
request.put("body", body);

Map<String, Object> params = new HashMap<>();
params.put("request", request);

// 查询参数转为json格式
String str = JSON.toJSONString(params);

String rs = post(URL_GET_SINGLE_DIAMOND, str);

return rs;

}

private static String post(String url, String strJsonParams) {
String rs = "";
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();

HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
post.setEntity(new StringEntity(strJsonParams, ContentType.create("application/json", "UTF-8")));

response = httpClient.execute(post);

if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
rs = EntityUtils.toString(entity);
}

response.close();
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
}

测试用例

import org.junit.Test;

public class RapNetTest {

private String username = "******";

private String password = "******";

private int diamondId = 1814612;

@Test
public void testGetDiamonds1() {
String rs = RapNet.getDiamonds(username, password, 1);
System.out.println(rs);
}

@Test
public void testGetDiamonds2() {
String rs = RapNet.getDiamonds(username, password, 3, 1);
System.out.println(rs);
}

@Test
public void testGetDiamonds3() {
String rs = RapNet.getDiamonds(username, password, 2, 2);
System.out.println(rs);
}

@Test
public void testGetSingleDiamond() {
String rs = RapNet.getSingleDiamond(username, password, diamondId);
System.out.println(rs);
}
}
分享到