用java发送POST-http请求

用java发送POST-http请求,java,curl,Java,Curl,将下一个curl请求转换为单个Java请求代码的最佳方法是什么 $ curl -i -H 'Content-Type: multipart/form-data' \ -H "Accept: application/json" \ -F "user[photo]=@test.jpg" \ -F "user[first_name]=Test" \ -F "user[password]=password" \ -F "user[email]=email@test.com" \

将下一个curl请求转换为单个Java请求代码的最佳方法是什么

$ curl -i
  -H 'Content-Type: multipart/form-data' \
  -H "Accept: application/json" \
  -F "user[photo]=@test.jpg" \
  -F "user[first_name]=Test" \
  -F "user[password]=password" \
  -F "user[email]=email@test.com" \
  -F "user[last_name]=Testing" \
  -X POST http://someURL.com:3000/api/user?client_id=zyBCF8N6yiJq8k
*我已经尝试了下一个:

    HttpPost postRequest = new HttpPost(AP("users", null));
    FileBody bin = new FileBody(new File(IMAGE_DIR + "/" + "nisbet-profile.jpg"));
    log.info("exporting file in path " + bin.getFile().getPath());
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("photo", bin);
    entity.addPart("first_name", new StringBody("test"));
    entity.addPart("last_name", new StringBody("test"));
    entity.addPart("email", new StringBody("testemail23@gmail.com"));
    entity.addPart("password", new StringBody("123456"));

    RestExporter picExporter = new RestExporter();
    postRequest.setEntity(entity);
    HttpResponse response = picExporter.request(postRequest);



    but I get: HTTP/1.1 422 Unprocessable Entity

    Any idea what I'm doing wrong?
查找
curl
选项的含义
-i,--include(HTTP)
-H、 --标题
-F、 --形式
-十、 --请求
想想这些选项意味着什么
  • 包括HTTP头
  • 您可以设置
    内容类型
    接受
    标题
  • 您可以设置表单字段
  • 您可以
    发布到URL
使用ApacheHttpComponents
  • 转到并阅读文档
  • 编写代码
重复
-i, --include (HTTP)
-H, --header <header>
-F, --form <name=content>
-X, --request <command>