i try to make a post request, witch contains a file, the problem is, i have to use ssl and the BasicCredentialprovider. The following code works for me with normal POST requests:
(url is the url, nvps is the nameValuePair)
DefaultHttpClient http = new DefaultHttpClient();
SSLSocketFactory ssl = (SSLSocketFactory)http.getConnectionManager().getSchemeRegistry().getScheme( "https" ).getSocketFactory();
ssl.setHostnameVerifier( SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER );
final String username = "xxx";
final String password = "xxx";
UsernamePasswordCredentials c = new UsernamePasswordCredentials(username,password);
BasicCredentialsProvider cP = new BasicCredentialsProvider();
cP.setCredentials(AuthScope.ANY, c);
http.setCredentialsProvider(cP);
HttpResponse res;
try {
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.DEFAULT_CONTENT_CHARSET));
res = http.execute(httpost);
InputStream is = res.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
res = null;
httpost = null;
return new String(baf.toByteArray());
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
return e.getMessage();
}
catch (IOException e) {
// TODO Auto-generated catch block
return e.getMessage();
}
I found the following code to perform a Fileupload, the code works, but i'm not able to combine the Upload with the autenthification an let him ignore the ssl errors (the cetificate is not valid)
String BOUNDRY = "==================================";
final String username = "xxx";
final String password = "xxx";
HttpURLConnection conn = null;
try {
String contentDisposition = "Content-Disposition: form-data; name="userfile"; filename="" + file.getName() + """;
String contentType = "Content-Type: application/octet-stream";
// This is the standard format for a multipart request
StringBuffer requestBody = new StringBuffer();
requestBody.append("--");
requestBody.append(BOUNDRY);
requestBody.append('
');
requestBody.append(contentDisposition);
requestBody.append('
');
requestBody.append(contentType);
requestBody.append('
');
requestBody.append('
');
requestBody.append(new String(getBytesFromFile(file)));
requestBody.append("--");
requestBody.append(BOUNDRY);
requestBody.append("--");
// Make a connect to the server
URL url = new URL(targetURL);
conn = (HttpURLConnection) url.openConnection();
// Put the authentication details in the request
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDRY);
// Send the body
DataOutputStream dataOS = new DataOutputStream(conn.getOutputStream());
dataOS.writeBytes(requestBody.toString());
dataOS.flush();
dataOS.close();
// Ensure we got the HTTP 200 response code
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
throw new Exception(String.format("Received the response code %d from the URL %s", responseCode, url));
}
// Read the response
InputStream is = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int bytesRead;
while((bytesRead = is.read(bytes)) != -1) {
baos.write(bytes, 0, bytesRead);
}
byte[] bytesReceived = baos.toByteArray();
baos.close();
is.close();
String response = new String(bytesReceived);
ret = response;
// TODO: Do something here to handle the 'response' string
} finally {
if (conn != null) {
conn.disconnect();
}
}
Any suggestions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…