集成到Java代码中后,我无法从eclipse中得到响应。我可以从邮递员/失眠中检索响应,但不能从日食中获取响应。我在此图像中屏蔽了令牌和URL。

java - 能够用 postman 打人但不能用Java打人-LMLPHP

我当前的代码是:

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并成功了

09-26 04:17