gpt4 book ai didi

java - 将表单数据作为文件发布请求 (wsdl.xml)

转载 作者:行者123 更新时间:2023-12-02 00:39:56 26 4
gpt4 key购买 nike

我正在尝试制作一个程序,该程序发送附加表单数据文件的发布请求,并将其作为发布请求的一部分上传。

我尝试了很多方法,但找不到页面。这里尝试使用多部分实体创建多部分主体并将对象传递给 http。

public void uploadwsdl() throws Exception {


MultipartEntityBuilder mb = MultipartEntityBuilder.create();
File file = new File("filpath");
mb.addBinaryBody("filename", file);
org.apache.http.HttpEntity fileEntity = mb.build();

String wsdlUrl = "url";
URL serverUrl = new URL(
wsdlUrl);

String CompanyCodeXML = getData("CompanyCodeMS");

System.out.println("=====Start XML Payload=====");

RestResponse response = retrieveStatusFromAPI(wsdlUrl,fileEntity);
Assert.assertEquals(response.getCode(), BaseHelper.HTTP_200, "Response Code is not same as Expected:");

String responseContent = response.getContent();

Assert.assertEquals(verifySuccessResponse(responseContent), true, "Response Message does not have JobId");

Assert.assertNotNull(response, "Response is null");

System.out.println("=====End XML Payload=====");

}


private RestResponse retrieveStatusFromAPI (String endPointURL,HttpEntity inputEntity)
{

RestResponse response = null;

try {
String userCredentials = "test" + ":" + "pwd";
String basicAuth = "Basic "+ new String(Base64.getEncoder().encode(userCredentials.getBytes()));

HttpRequests connection = new HttpRequests();

response = connection.httpPost(endPointURL, basicAuth, inputEntity);

} catch (Exception ioe) {
ioe.printStackTrace();

}

return response;
}

最佳答案

我可以找到一个解决方案来发送带有文件作为表单数据的发布请求。只需要修改EntityBuilder代码如下:

final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("filename", file, ContentType.DEFAULT_BINARY, "some.xml");
final HttpEntity multiPartEntity = builder.build();

关于java - 将表单数据作为文件发布请求 (wsdl.xml),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57950814/

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