I am trying to download file from a https website using java HttpsURLConnection with the Authorization header. I keep getting back the 401 response code, however, I don't have a problem using the credentials to download it through a web browser. Please help to take a look what might be wrong with my code:
File file = new File("c:/filename");
String src = "https://www.example.com/filename";
try {
file.createNewFile();
URL url = new URL(src);
HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();
String userCredentials = "username:password";
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
urlConnection.addRequestProperty ("Authorization", basicAuth);
urlConnection.connect();
System.out.println("code" + urlConnection.getResponseCode());
InputStream in = urlConnection.getInputStream();
OutputStream out = new FileOutputStream(file);
int d;
while ((d = in.read()) != -1) {
out.write(d);
}
in.close();
out.close();
} catch (Exception e) {
System.err.println("Failed to get the file from source. " + e);
if (file.exists()) {
file.delete();
}
}
Related
I'm trying to connect to an API that has a username and password with this code:
try {
URL url = new URL("https://url?UserName=username&Password=password");
Connection = (HttpURLConnection) url.openConnection();
//Request setup
Connection.setRequestMethod("GET");
Connection.setConnectTimeout(5000);
Connection.setReadTimeout(5000);
int status = Connection.getResponseCode();
System.out.println(status);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
But I get the error:
java.net.SocketException: Connection reset
I kept searching and I found another format for the URL which is:
URL url = new URL("Https:username:password#url);
When I tried, it gave me the error:
java.net.MalformedURLException: For input string:password#url
I tried to separate the URL into three strings and made the password Integer.pharsInt("String"), but it also didn't work.
The password has words, numbers, and a special character!
What am I doing wrong?
Try to encode your URL, this way:
HttpURLConnection connection = (HttpURLConnection)
new URL("https://url?UserName" +
URLEncoder.encode(username, "UTF-8") +
"&Password=" +
URLEncoder.encode(password, "UTF-8"))
.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// The rest of your code
private void uploadDocToSharePoint(String token, Resource resource, String folderName) {
try {
String uploadUrl = Utils.SHARE_POINT_DOMAIN + "_api/web/getfolderbyserverrelativeurl('" + folderName + "')/files/add(url='" + resource.getFilename() + "', overwrite=true)";
URL url = new URL(uploadUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// Set Header
httpConn.setDoOutput(true);
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Authorization", "Bearer " + token);
httpConn.setRequestProperty("accept", "application/json; odata=verbose");
httpConn.setRequestProperty("Content-Type", "application/xml");
OutputStream os = httpConn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
osw.write("Just Some Text");
osw.flush();
osw.close();
os.close(); //don't forget to close the OutputStream
httpConn.connect();
System.out.println(httpConn.getResponseCode());
System.out.println(httpConn.getResponseMessage());
String result;
BufferedInputStream bis = new BufferedInputStream(httpConn.getInputStream());
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result2 = bis.read();
while(result2 != -1) {
buf.write((byte) result2);
result2 = bis.read();
}
result = buf.toString();
System.out.println(result);
} catch (Exception e) {
System.out.println("Error while reading file: " + e.getMessage());
}
}
httpConn.getResponseCode() is 400 and httpConn.getResponseMessage() is Bad Request.
I have tested this request with the URL generated in this class on Postman.
it works correctly.
so I am sure about url and token is correct.
It creates an Empty file successfully.
But as I mentioned the response status is 400 and Bad Request.
I am not sure what is wrong with the following class
Sharepoint guide here
My class copied from here
screenshot here
Pls follow Sharepoint guide here
I am trying to uploading chunks of 256 KB in Google Drive using REST API v3. I can successfully get the upload ID but when I use this upload ID to upload a chunk, I get a 400 Bad Request instead of 308. I am posting the code below. The method getUploadID() initiates a resumable upload session and the method putFileWithUploadID() should upload a chunk of the file but there seems to be a problem in it. I have written the code according to the official guide.
String mUploadID;
private String getUploadID(Uri fileUri, String token) {
String upload_id = "";
java.io.File fileContent = new java.io.File(fileUri.getPath());
String fileName = fileContent.getName();
String mimeType = "audio/mpeg";
try {
String url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Authorization", "Bearer " + token);
con.setRequestProperty("X-Upload-Content-Type", mimeType);
con.setRequestProperty("X-Upload-Content-Length", String.format(Locale.ENGLISH, "%d", fileContent.length()));
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
String body = "{\"name\": \"" + fileName + "\", \"parents\": [\"" + "0B7ypsm4HGZhCS3FXcldPZnFPNkE" + "\"]}";
con.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", body.getBytes().length));
OutputStream outputStream = con.getOutputStream();
outputStream.write(body.getBytes());
outputStream.close();
con.connect();
String location = con.getHeaderField("Location");
if (location.contains("upload_id")) {
String[] uploadParameters = location.split("upload_id");
upload_id = uploadParameters[1].replace("=", "");
}
} catch (Exception e) {
e.printStackTrace();
}
return upload_id;
}
private void putFileWithUploadID(Uri fileUri, String token, long range) {
java.io.File fileContent = new java.io.File(fileUri.getPath());
String fileName = fileContent.getName();
String contentLength = String.valueOf(fileContent.length());
String mimeType = "audio/mpeg";
long totalBytesFromDataInputStream = 0;
long uploadedBytes = 0;
long chunkStart = 0;
long chunkSize = 262144;
do {
try {
String url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=" + mUploadID;
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("PUT");
con.setDoOutput(true);
con.setConnectTimeout(10000);
con.setRequestProperty("Content-Type", mimeType);
uploadedBytes = chunkSize;
if (chunkStart + uploadedBytes > fileContent.length()) {
uploadedBytes = (int) fileContent.length() - chunkStart;
}
con.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", uploadedBytes));
con.setRequestProperty("Content-Range", "bytes " + chunkStart + "-" + (chunkStart + uploadedBytes - 1) + "/" + fileContent.length());
byte[] buffer = new byte[(int) uploadedBytes];
FileInputStream fileInputStream = new FileInputStream(fileContent);
fileInputStream.getChannel().position(chunkStart);
if (fileInputStream.read(buffer, 0, (int) uploadedBytes) == -1) {
break;
}
fileInputStream.close();
OutputStream outputStream = con.getOutputStream();
outputStream.write(buffer);
outputStream.close();
con.connect();
int responseCode = con.getResponseCode();
String rangeHeader = con.getHeaderField("Range");
if (rangeHeader!=null) {
chunkStart = Long.parseLong(rangeHeader.substring(rangeHeader.lastIndexOf("-") + 1, rangeHeader.length())) + 1;
}
} catch (Exception e) {
e.printStackTrace();
}
} while ((chunkStart+chunkSize)<fileContent.length());
}
Basically, 400: Bad Request means that a required field or parameter has not been provided, the value supplied is invalid, or the combination of provided fields is invalid.
This error can be thrown when trying to add a duplicate parent to a Drive item. It can also be thrown when trying to add a parent that would create a cycle in the directory graph.
You may also check this related SO post which suggested to properly use the Android-specific API to do resumable upload. See creating files.
I am really stuck on this problem and hoping for some help. I automatically log in to a Server and download a Excel (xlsx) File. The download seems to work fine, cause the size is the same as on the server and if I open the file with an XML-Editor it looks valid (compared to other excel Files).
But if I try to open the downloaded file with excel I got a "The file might be corrupt" message and it's not readable.
I tried to change the encoding of the file, the headers of the GET-Request, even tried to write it as Bytestream. Nothing changed.
Here are some relevant code snippets:
public HttpsURLConnection login()
{
HttpsURLConnection connection = null;
String user = "user";
String password = "password";
String authString = user + ":" + password;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
try {
connection = (HttpsURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//connection.setRequestProperty("Content-Type", "application/vnd.openxml");
connection.setRequestProperty("Accept", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//connection.setRequestProperty("Accept", "application/vnd.openxml");
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.connect();
} catch (IOException e)
{
e.printStackTrace();
}
return connection;
}
The download class
private void sendGet(HttpsURLConnection con) throws Exception {
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
String fileName = "test.xlsx";
file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
//use FileWriter to write file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
//BufferedWriter bw = new BufferedWriter(fw);
/**CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);**/
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8"
));
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
bw.write(inputLine);
}
in.close();
bw.close();
//print result
//System.out.println(response.toString());
}
You can see my attempts in some of the commented lines.
I appreciate any help. Thanks in advance.
This is the method I have in my java application. It is reading the bytes correctly, I have logged to see if it was. The problem is that the php is not realizing the data is there. I have tested and the .php reads that $_POST is set, but is empty.
public void screenshot(BufferedImage screenshot) {
try {
ImageIO.write(screenshot, "png",
new File(Environment.getStorageDirectory().toString()
.concat(File.separator + SCRIPT_NAME + ".png")));
HttpURLConnection httpUrlConnection;
OutputStream outputStream;
BufferedInputStream fileInputStream;
BufferedReader serverReader;
int totalBytes;
String response = "";
String serverResponse = "";
String localFileName = Environment.getStorageDirectory().toString()
.concat(File.separator + SCRIPT_NAME + ".png");
// Establish a connection
httpUrlConnection = (HttpURLConnection) new URL(
"http://www.scripted.it/scriptoptions/utils/saveScreenshot.php?user="
+ SupraCrafter.statHandler.getUser())
.openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
outputStream = httpUrlConnection.getOutputStream();
// Buffered input stream
fileInputStream = new BufferedInputStream(new FileInputStream(
localFileName));
// Get the size of the image
totalBytes = fileInputStream.available();
// Loop through the files data
for (int i = 0; i < totalBytes; i++) {
// Write the data to the output stream
outputStream.write(fileInputStream.read());
}
// Close the output stream
outputStream.close();
// New reader to get server response
serverReader = new BufferedReader(new InputStreamReader(
httpUrlConnection.getInputStream()));
// Read the servers response
serverResponse = "";
while ((response = serverReader.readLine()) != null) {
serverResponse = serverResponse + response;
}
System.out.println(serverResponse);
// Close the buffered reader
serverReader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
URL url = new URL(
"http://scripted.it/scriptoptions/utils/setScreenshotStatus.php?user="
+ SupraCrafter.statHandler.getUser() + "&pass="
+ SupraCrafter.statHandler.getPass() + "&script="
+ SCRIPT_NAME + "&status=1");
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
Here is the .php file:
<?
// Config
$uploadBase = "../screenshots/";
$uploadFilename = $_GET['user'] . ".png";
$uploadPath = $uploadBase . $uploadFilename;
// Upload directory
if(!is_dir($uploadBase))
mkdir($uploadBase);
// Grab the data
$incomingData = file_get_contents('php://input');
// Valid data?
if(!$incomingData)
die("No input data");
// Write to disk
$fh = fopen($uploadPath, 'w') or die("Error opening file");
fwrite($fh, $incomingData) or die("Error writing to file");
fclose($fh) or die("Error closing file");
echo "Success";
?>
It always echos 'no input data.'
You are not encoding the content with application/x-www-form-urlencoded. You should not simply copy the bytes into the HTTP payload, but instead encode it correctly.
application/x-www-form-urlencoded is not the only possible way of encoding it, multipart/form-data is another common choice. Both are supported by almost all webservers, and as a consequence by PHP.
A tutorial on how to encode using Java is here : http://www.devx.com/Java/Article/17679
Why don't you use Apache's HttpClient or similar library that already do that tedious work for you?
Apache HttpClient : http://hc.apache.org/httpcomponents-client-ga/