我必须针对一个特定参数测试具有不同内容长度(REST
)的API
Post
(HTTPS
& 1KB-10MB
)。
此API的目的是发送文件。 Rest
请求正常工作,直到我发送1-5KB文件。
但是,除了那个请求失败之外,实际上它似乎从未被发送过。我在下面的XML响应中看到,json下没有任何内容
data contentType =" null" CONTENTLENGTH =" 0">
我已经尝试过标题和内容中的Content-Length;增加套接字超时
我也尝试过浏览器插件,但在发送大字符串
时也失败了答案 0 :(得分:0)
我遇到了同样的问题。但是我通过groovy脚本解决了上传大文件的问题。这是可以帮助你的代码。
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
String filename = "D:\\file.txt"
String hosts = "some-endpoint.com"
String resource = "resource/to/upload"
//Complete URL
String url = "http://" + hosts + "/" + resource;
log.info(url)
File file = new File(filename);
FileEntity reqEntity = new FileEntity(file, "text/plain");
DefaultHttpClient client = new DefaultHttpClient();
HttpPut req = new HttpPut(url);
req.setHeader("header-key", "header-value");
// here set the file entity
req.setEntity(reqEntity);
//Declare HttpResposne object and intialize it to null
HttpResponse resp = null;
try {
resp = client.execute(req);
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
log.info(resp);
HttpEntity entityNew = resp.getEntity();
System.out.println(resp.toString());
if(resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
entityNew.writeTo(out);
} catch (IOException e) {
e.printStackTrace();
}
String str = new String(out.toByteArray());
log.info(str);
if(str.contains("some_text_in_response")){
System.out.println("succ");
assert true;
}
else{
System.out.println("Fail");
assert false;
}
}
else{
System.out.println("Fail");
assert false;
}
我用过HttpPut把你可以HttpPost,HttpGet等方法类引用apche http-client library docs