我正在尝试将java应用程序连接到betaface的json api。 我遇到了一个奇怪的问题,希望你们能帮助我。
我有(丑陋)代码连接到api。这段代码只是为了测试我得到了什么样的响应以及如何处理它。
我构建了一个请求体,将请求体打印到控制台,然后将请求体写入api。问题是响应码是400。
奇怪的是,当我复制请求主体并通过Advanced Rest Client执行它时,它将返回200和预期的主体。我认为问题不在于身体,而是在HTTPUrlConnection的某个地方。
我已经添加了我的测试类来说明问题,你们只需要一个小图像来重现问题。 API密钥是免费使用betaface的api密钥。
非常感谢您给我的任何帮助。
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;
import javax.imageio.ImageIO;
public class APITest {
/**
* @param args
*/
public static void main(String[] args) {
try
{
new APITest();
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public APITest() throws MalformedURLException, IOException {
String api_key = "d45fd466-51e2-4701-8da8-04351c872236";
String api_secret = "171e8465-f548-401d-b63b-caf0dc28df5f";
String urlToConnect = "http://www.betafaceapi.com/service_json.svc/UploadNewImage_File";
File fileToUpload = new File("C:\\test.jpg");
byte[] t = getImageBase64ByteArray(fileToUpload);
String body = "";
body += "{\"api_key\":\"";
body += api_key;
body += "\",\"api_secret\":\"";
body += api_secret;
body += "\",\"detection_flags\":\"\",\"imagefile_data\":[";
for(int i = 0; i < t.length; i++) {
body += t[i];
if(i < t.length - 1)
body += ",";
}
body += "],\"original_filename\":\"Test.jpg\"}";
System.out.println(body);
HttpURLConnection connection = (HttpURLConnection)new URL(urlToConnect).openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
writer.write(body);
writer.newLine();
writer.flush();
writer.close();
int responseCode = connection.getResponseCode();
System.out.println(responseCode); // Should be 200
InputStream errorstream = connection.getErrorStream();
BufferedReader br = null;
if (errorstream == null){
InputStream inputstream = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(inputstream));
}else{
br = new BufferedReader(new InputStreamReader(errorstream));
}
String response = "";
String line;
while ((line = br.readLine()) != null){
response += line;
}
System.out.println(response);
}
public byte[] getImageBase64ByteArray(File file) {
BufferedImage bufferedImage;
try
{
bufferedImage = ImageIO.read(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
String encoded = "";
try {
ImageIO.write(bufferedImage, "jpg", bos);
byte[] imageBytes = bos.toByteArray();
encoded = Base64.getEncoder().encodeToString(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return encoded.getBytes();
}
catch(IOException e)
{
e.printStackTrace();
}
return new byte[0];
}
}