【发布时间】:2016-06-07 12:11:05
【问题描述】:
我在将文件上传到服务器时遇到问题。这里我正在尝试创建注册表单。
我需要上传从用户那里获取的所有值,以及我需要上传 PDF 格式的简历。
这是我的代码。请仔细查看。
public String serverResponse(String mFilePath){
HttpClient client = new DefaultHttpClient();
HttpPost poster = new HttpPost(mUrl);
File resume = new File(mFilePath); //Actual file from the device
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("name", new StringBody("name"));
entity.addPart("phone", new StringBody("1234567890"));
entity.addPart("attachment", new FileBody(resume));
poster.setEntity(entity);
return client.execute(poster, new ResponseHandler<String>() {
public String handleResponse(HttpResponse response) throws IOException {
HttpEntity respEntity = response.getEntity();
return EntityUtils.toString(respEntity);
}
});
}
问题是当我将数据发送到 url("http://www.example.com") 时,上面的代码有效,但它不适用于 url("https://www.example.com")。
谁能告诉我的代码有什么问题。
请帮帮我。
编辑:我在服务器端检查了来自 android 的请求,我在请求中发现了空数据,它以默认消息响应(在服务器中设置的响应)。
so my request hits the server with empty values. Is problem in my code (or) server side ?
刚才我检查了一下,这个相同的 URL 在网站上可以正常工作。
如果我错了,请以正确的方式指导我
提前致谢。
【问题讨论】:
-
我猜,也许你的
httpsurl 没有用 ssl 证书签名,服务器可能用SSLHandshakeException响应。如果您遇到任何异常,请检查您的堆栈跟踪 -
但我将数据(无文件)发送到具有相同域的服务器并且它可以工作。您说 SSL 未登录服务器,您可以通过参考链接指导我。
-
@sripadRaj 我已经更新了我的问题,请参阅我的编辑
-
我不确定,不能说你的代码是错误的还是服务器端的问题。用 try catch 块包围你的代码,检查你是否遇到任何异常。还要检查来自服务器的响应代码。
标签: java android file-upload https apache-httpclient-4.x