我对软件开发很陌生,而且我正在开发一个项目,该项目使用持票令牌对交易服务器进行API调用。
我已经弄明白了如何通过命令行通过curl调用API。它看起来像这样(它是通过http而不是https,因为我使用的测试服务器只支持http请求)
curl http://api.testwebsite.com/transactions/ \
-H "Authorization: Bearer <TOKEN ID>" \
-d amount=500 \
-d currency="USD" \
-d card_number="<card number>" \
-d card_exp_month=<exp month> \
-d card_exp_year=<exp year> \
-d card_cvv="<card's ccv>"
现在我用Java编写程序(必须是Java),只使用本机Java库(没有像Spring或Apache这样的第三方软件包),我试图通过HttpUrlConnection调用API。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.DataOutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import java.net.HttpURLConnection;
public class TestHttpsPost {
public static void main(String[] args) throws Exception {
// Uncomment one of these next two lines:
URL url = new URL("http://api.testwebsite.com/transactions");
//URL url = new URL("https://httpbin.org/post"); // good for testing
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
// Change this to a valid token:
connection.setRequestProperty("Authorization", "Bearer <TOKEN ID>");
/*
connection.setRequestProperty("amount", "500");
connection.setRequestProperty("currency", "USD");
connection.setRequestProperty("card_number", "<CARD_NUMBER>");
connection.setRequestProperty("card_exp_month", "<EXP_MONTH>");
connection.setRequestProperty("card_exp_year", "<EXP_YEAR");
connection.setRequestProperty("card_cvv", "<CCV>");
*/
String jsonData1 = "amount=500";
String jsonData2 = "currency=\"USD\"";
String jsonData3 = "card_number=\"11111111111111111\"";
String jsonData4 = "card_exp_month=11";
String jsonData5 = "card_exp_year=2011";
String jsonData6 = "card_cvv=\"123\"";
try {
// Post the data:
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
output.writeBytes(jsonData1);
output.writeBytes(jsonData2);
output.writeBytes(jsonData3);
output.writeBytes(jsonData4);
output.writeBytes(jsonData5);
output.writeBytes(jsonData6);
output.close();
// Read the response:
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("Response code:" + connection.getResponseCode());
System.out.println("Response message:" + connection.getResponseMessage());
}
}
但我一直在
Server returned HTTP response code: 400 for URL: http://api.testwebsite.com/transactions
Response code:400
Response message:Bad Request
我跟着Transforming a PayPal curl request to an http request in Java,但是paypal api的字段非常不同,我不确定我是否搞砸了。
提前致谢!
答案 0 :(得分:0)
非常好的选择是使用Retrofit(java和Android的类型-safe http客户端)
http://square.github.io/retrofit/
这是使用身份验证令牌服务的示例:
public class ServiceGenerator {
public static final String BASE_URL = "https://your.api.url";
// returns an API client of type serviceClass
public static <S> S createService(
Class<S> serviceClass, final String token) {
RestAdapter.Builder builder = new RestAdapter.Builder() .setEndpoint(BASE_URL)
.setClient(new OkClient(new OkHttpClient()));
if (token != null) {
builder.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) { request.addHeader("Accept", "application/json"); request.addHeader("Authorization", token);
} });
}
RestAdapter adapter = builder.build();
return adapter.create(serviceClass); }
}