我是JSON新手,正在研究Coinbase API。 See here。如何在Java中通過HTTP POST JSON數據?
所以我試圖發佈此數據通過HTTP:
{
"transaction": {
"to": "[email protected]",
"amount": "1.234",
"notes": "Sample transaction for you"
}
}
我將如何做到這一點在Java中?
我知道如何創建一個程序來GET
一個響應但不POST
如何將要求。
這裏是我的響應計劃:
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import com.coinbase.api.Coinbase;
import com.coinbase.api.CoinbaseBuilder;
public class CoinbaseExample {
static String API_KEY = "My API KEY";
static String API_SECRET = "MY API SECRET";
public static String getHttp(String url, String body)
throws InvalidKeyException, NoSuchAlgorithmException,
ClientProtocolException, IOException {
String nonce = String.valueOf(System.currentTimeMillis());
String message = nonce + url + (body != null ? body : "");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(API_SECRET.getBytes(), "HmacSHA256"));
String signature = new String(Hex.encodeHex(mac.doFinal(message.getBytes())));
HttpRequestBase request;
if (body == null || body.length() == 0)
request = new HttpGet(url);
else {
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(body));
request = post;
}
request.setHeader("ACCESS_KEY", API_KEY);
request.setHeader("ACCESS_SIGNATURE", signature);
request.setHeader("ACCESS_NONCE", nonce);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null)
return EntityUtils.toString(entity);
return null;
}
}
如何創建此類似程序後要求?
你嘗試過自己? – 2014-12-03 08:56:15
亞..我試過自己..但總是卡住! :(這就是爲什麼我來這裏問... :(請幫助! – 2014-12-03 08:57:55
解釋這個問題,你得到什麼?一個錯誤?請求沒有發送,它是空的?... – Yazan 2014-12-03 09:04:52