gpt4 book ai didi

java - 在postman中可以命中,但在java中不行

转载 作者:行者123 更新时间:2023-12-01 17:18:59 29 4
gpt4 key购买 nike

集成到 java 代码中时,我无法从 eclipse 获得响应。我能够从 postman /失眠中检索到响应,但不能从 eclipse 中检索到响应。我屏蔽了该图像中的 token 和 URL。

Postman Pictire

我当前的代码是:

public class Test{

public static void main(String[] args) throws ParseException, IOException {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

httppost.addHeader("Authorization", "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
httppost.addHeader("Accept", "*/*");
httppost.addHeader("Content-type", "multipart/form-data; boundary=X-INSOMNIA-BOUNDARY");

httppost.addHeader("Host","process-workorders-mti64mke4a-uc.a.run.app");



File fileToUse = new File("D:\\firstImage.jpg"); // this is the image I am uoploadin
FileBody data = new FileBody(fileToUse);


MultipartEntity reqEntity = new MultipartEntity();

reqEntity.addPart("mode", new StringBody("api"));
reqEntity.addPart("file", data);
*// seems there is issue here in passing form parameters*

httppost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httppost);
System.out.println( response ) ;

HttpEntity resEntity = response.getEntity();
System.out.println( resEntity ) ;
System.out.println( EntityUtils.toString(resEntity) );

EntityUtils.consume(resEntity);
httpclient.getConnectionManager().shutdown();
}
}

以下是导入内容:

import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

最佳答案

尝试了 OKHTTPClient 并且成功了

关于java - 在postman中可以命中,但在java中不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61334315/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com