Java -HTTP URL connection downloaded file with empty - java

Trying to downloading a file from url (this service is developed in .net soap request) using java (HttpURLConnection class) i'm getting HttpURLConnection.HTTP_OK file successfully downloaded with empty content (i.e 0kb)
Code:
String userCredentials = "abc:cde";
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestProperty("Authorization","Basic "+ userCredentials);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "Application/octet-stream");
connection.setChunkedStreamingMode(4096);
connection.setRequestProperty("SOAPAction", url.toString());
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
String saveFilePath = saveDir + File.separator + downloadFileName;
System.out.println(saveFilePath);
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead ;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
Output:
HTTPResponse code : 200
File downloaded with empty
Note: response header
{null=[HTTP/1.1 200 OK], Server=[Microsoft-IIS/8.5], Cache-Control=[private], X-AspNet-Version=[4.0.30319], Content-Length=[0], Date=[Fri, 15 Jun 2018 09:14:07 GMT], X-Powered-By=[ASP.NET]}

You're not connecting resources.
Add connection.connect() before your responseCode = httpurlconnection.http_oksentence.

Related

Upload File Using HTTP Post - Java

I have this current code. The file is in memory on the InputStream in or in test.pdf. I would prefer to only keep in-memory.
FileOutputStream fos = new FileOutputStream(new File("test.pdf"));
// Read file
InputStream in = url.openStream();
while((bufferLength = in.read(buffer)) != -1) {
fos.write(buffer, 0, bufferLength);
}
fos.flush();
// Close connections
fos.close();
in.close();
System.out.println("GOT DOCUMENT");
String submitURl = "https://someURL/submit";
// Send data
HttpURLConnection conn = (HttpURLConnection) new URL(submitURl).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type","multipart/form-data");
conn.setRequestProperty("User-Agent", "Test Agent");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.flush();
How do I post this file to the /submit URL. What am I missing here?

Java HttpUrlConnection don't read full response body

I can read full response body (big JSON data, more than 400,000 chars) few times, but after 5-6 times my response is not full.
Here's my code of getting response:
URL address = new URL("https://myurl.com/");
HttpURLConnection Connection = (HttpURLConnection)address.openConnection();
Connection.setRequestMethod("GET");
Connection.setRequestProperty("accept-language", "en-US");
Connection.setRequestProperty("user-agent", UserAgent);
Connection.setRequestProperty("cookie", cookies);
Connection.setUseCaches(false);
Connection.setDoInput(true);
Connection.setDoOutput(true);
if (Connection.getResponseCode() == HttpsURLConnection.HTTP_OK)
{
String Response = new String();
InputStream is = Connection.getInputStream();
int ch;
StringBuffer sb = new StringBuffer();
while (( ch = is.read()) != -1) {
sb.append((char) ch);
}
Response = sb.toString();
is.close();
}
In my original code after is.close(), it is just lot of JSON parsing from Response string

Getting unicode results in MailChimp oauth2 token creation

I have generated the access code by using https://login.mailchimp.com/oauth2/authorize API. But when I try to create the token using https://login.mailchimp.com/oauth2/token, I'm getting unicode result like this.
(?M?? ?0F?UJ?N?NQ? %`??'
"?????nb??f=?&9????i'f??]?~j*$??W??Reg??_T1-???;?oc)
qryStr = {"client_secret":"**********","grant_type":"authorization_code","redirect_uri":"https%3A%2F%2Flocalhost%3A9443%2Fverifymailchimp.sas","client_id":"********","code":"*************"}
HttpURLConnection connection = null;
try
{
URL reqURL = new URL("https://login.mailchimp.com/oauth2/token");
connection = (HttpURLConnection) reqURL.openConnection();
connection.setConnectTimeout(3000); // 3 seconds
connection.setReadTimeout(5000); // 5 seconds
connection.setUseCaches(false);
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //No I18N
connection.setRequestProperty("Content-Length", "" + Integer.toString(qryStr.getBytes().length)); //No I18N
connection.setDoOutput(true);
OutputStream os = null;
try
{
os = connection.getOutputStream();
os.write(qryStr.getBytes(CHARSET));
}
finally
{
try{os.close();}catch(Exception e){}
}
int resCode = connection.getResponseCode();
boolean success = (resCode >= 200 && resCode < 300);
InputStream is = success ? connection.getInputStream() : connection.getErrorStream();
if (is == null)
{
return null;
}
String contentStr = null;
try
{
InputStreamReader reader = new InputStreamReader(is, CHARSET);
StringBuilder buffer = new StringBuilder();
char[] bytes = new char[1024];
int bytesRead;
while ((bytesRead = reader.read(bytes, 0, bytes.length)) > 0)
{
buffer.append(bytes, 0, bytesRead);
}
contentStr = buffer.toString();//?M?? ?0F?UJ?N?NQ? %`??' "?????nb??f=?&9????i'f??]?~j*$??W??Reg??_T1-???;?oc
}
finally
{
try{is.close();}catch(Exception e){}
}
}
Can anyone please tell the cause?
I found the cause of this case. An access code is valid for 30 seconds. Need to generate the token before the expiry. If they conveyed the proper error message, we can able to sort out the problem without any confusion :(

Android: java.lang.IllegalStateException: Already connected

I'm testing a sample of code but its always error at connection.setDoInput(true);
HttpsURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String urlServer = "https://www.myurl.com/upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead = 0;
int bytesAvailable = 0;
int bufferSize = 0;
byte[] buffer = null;
int maxBufferSize = 1*1024*1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(params[0]));
URL url = new URL(urlServer);
connection = (HttpsURLConnection) url.openConnection();
connection.setConnectTimeout(1000);
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setConnectTimeout(1000);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + params[0] + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
The error log is java.lang.IllegalStateException: Already connected.
I have tried these but none is working:
connection.setRequestProperty("Connection", "close");
connection.disconnect();
connection.setConnectTimeout(1000);
EDIT: even when i didn't call connection.connect(), it's still giving the same error already connected.
You must close the input stream after reading it to end of stream.
You should remove the call to connect(). You have it in the wrong place, but it's automatic and doesn't need to be called at all.
You can also remove the line that sets POST. This is implicit in calling setDoOutput(true).
You can also remove most of that crud in the copy loop. Use a fixed size buffer:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
Do not use a new buffer per read; do not call available(); do not pass GO; do not collect $200.
Move
connection.connect();
after
connection.setRequestMethod("POST");
There is a very good post about http connections here
The bottom line is that for POSTs you only need the following.
HttpURLConnection connection = (HttpURLConnection) new URL(urlToRead).openConnection();
// setDoOutput(true) implicitly set's the request type to POST
connection.setDoOutput(true);
I'm not sure you need to specify HttpsURLConnection either. You can use HttpURLConnection for connecting to Https sites. Let java do the work for you behind the scenes.
Here is the POST code that I use for json posts
public static String doPostSync(final String urlToRead, final String content) throws IOException {
final String charset = "UTF-8";
// Create the connection
HttpURLConnection connection = (HttpURLConnection) new URL(urlToRead).openConnection();
// setDoOutput(true) implicitly set's the request type to POST
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-type", "application/json");
// Write to the connection
OutputStream output = connection.getOutputStream();
output.write(content.getBytes(charset));
output.close();
// Check the error stream first, if this is null then there have been no issues with the request
InputStream inputStream = connection.getErrorStream();
if (inputStream == null)
inputStream = connection.getInputStream();
// Read everything from our stream
BufferedReader responseReader = new BufferedReader(new InputStreamReader(inputStream, charset));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = responseReader.readLine()) != null) {
response.append(inputLine);
}
responseReader.close();
return response.toString();
}
Add
if (connection != null) connection.disconnect();
before
connection = (HttpsURLConnection) url.openConnection();
See if problem solved. If yes, it means connection.disconnect() is not called in your original code. Maybe you put connection.disconnect() at the end of your try block, however an exception occurs before it, so it jumps to the catch block and connection.disconnect() is never called.

File is not uploded after removing System.out.println("response :: " + conn.getResponseMessage());

Following is my function to upload file GCS :
public void fileUpload(InputStream streamData, String fileName,
String content_type) throws Exception {
byte[] utf8Bytes = fileName.getBytes("UTF8");
fileName = new String(utf8Bytes, "UTF8");
URL url = new URL("http://bucketname.storage.googleapis.com"+"/"+"foldername/"+fileName);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Accept", "application/x-www-form-urlencoded");
conn.setRequestProperty("Authorization", "OAuth " + GcsToken.getAccessToken());
conn.setRequestProperty("x-goog-meta-FileName", fileName);
conn.setRequestProperty("x-goog-meta-ContentType", content_type);
OutputStream os = conn.getOutputStream();
BufferedInputStream bfis = new BufferedInputStream(streamData);
byte[] buffer = new byte[1024];
int bufferLength = 0;
// now, read through the input buffer and write the contents to the file
while ((bufferLength = bfis.read(buffer)) > 0) {
os.write(buffer, 0, bufferLength);
}
System.out.println("response :: " + conn.getResponseMessage());// ?????
}
This code works fine to uplaod file, but
After removing last Sysout , it is not uploading file
System.out.println("response :: " + conn.getResponseMessage());
what is reason behind this ?
any help ?
thnaks
You need to close your OutputStream to indicate that you've finished writing the request body:
os.close();
You should also check the response code of the request:
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
// Error handling code here.
}
The reason it was working before is because the getResponseMessage function blocks until the request is finished being sent and the reply is received. Without ever checking the response value, your function just exits and the HTTP request might not be finished sending.
Thanks for this clarification.
I already tried with os.close() also tried with os.flush(). but same problem. :(
at last i have updated my code:
while ((bufferLength = bfis.read(buffer)) > 0) {
os.write(buffer, 0, bufferLength);
}
os.close();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
//logger
}
Now I am able to upload file.
Thanks again.

Categories

Resources