I'm using http client to get data:
public static String getHttpResponse(String url) {
//LOGGER.info("Download page context from URL " + url);
String httpClientResponse = null;
try {
URI uri = new URIBuilder(url).build();
HttpResponse response;
HttpHost target = new HttpHost(uri.getHost());
HttpGet request = new HttpGet(uri);
//request.setConfig(config);
request.addHeader(new BasicHeader("User-Agent", "Mozilla/5.0"));
request.addHeader(new BasicHeader("Content-Type", "text/html"));
request.addHeader("Accept-Ranges", "bytes=100-1500");
org.apache.http.client.HttpClient
client = HttpClients.custom().build();
response = client.execute(target, request);
//LOGGER.info("Status Line for URL {} is {}", uri.getHost() + File.separator + uri.getPath(), response.getStatusLine());
InputStream inputStream = response.getEntity().getContent();
if (inputStream == null || response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
/*LOGGER.error("Non-success response while downloading image. Response {}", response.getStatusLine());
LOGGER.error("Error while download data from url {}", url);*/
} else {
httpClientResponse = IOUtils.toString(inputStream, CharEncoding.UTF_8);
}
} catch (Exception e) {
System.out.println("Error while download content from URL");
}
return httpClientResponse;
}
Also: Can we do this using Jsoup?
Thanks.
Replace:
request.addHeader("Accept-Ranges", "bytes=100-1500");
with:
request.addHeader("Range", "bytes=100-1500");
The Accept-Ranges header is part of server response, which indicates that the server accepts partial requests.
In your request you should use Range header, which indicates which part of document server should return.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Ranges
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
Related
I have an issue updating a docx file on Sharepoint online with Java.
First I checked the URL to build the PUT request (here : https://learn.microsoft.com/fr-fr/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http) and use this request : PUT /drives/{drive-id}/items/{item-id}/content
I first use service to build the URL :
#Override
public UpdateDocumentResponseModel updateDocument(String accessToken, String libId, String docId, String filePath) throws MalformedURLException {
URL url = new URL("https://graph.microsoft.com/v1.0/drives/" + libId + "/items/" + docId + "/content");
return documentRequestBuilder.updateDocument(accessToken, url, filePath);
}
I build the request :
public UpdateDocumentResponseModel updateDocument(String accessToken, URL url, String fullPath) {
UpdateDocumentResponseModel returnValue = new UpdateDocumentResponseModel();
Tika tika = new Tika();
String mimeType = tika.detect(fullPath);
System.out.println("Test mime type: " + mimeType);
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(String.valueOf(url));
httpPut.setHeader("Authorization", "Bearer " + accessToken);
httpPut.setHeader("Content-Type", mimeType);
httpPut.setHeader("Connection", "Keep-Alive");
httpPut.setHeader("Cache-Control", "no-cache");
// read the file and convert to stream
// Get an input stream for the file
InputStream in = new FileInputStream(fullPath);
httpPut.setEntity(new StringEntity(String.valueOf(in), StandardCharsets.UTF_8));
CloseableHttpResponse response = client.execute(httpPut);
System.out.println("\nSending 'PUT' request to URL : " + url);
System.out.println("Response Code : " + response.getStatusLine());
// set the response
returnValue.setDocumentName(fullPath);
returnValue.setUpdatedAt(new Date());
returnValue.setUpdateStatus("Success");
} catch (IOException e) {
returnValue.setDocumentName(fullPath);
returnValue.setUpdatedAt(new Date());
returnValue.setUpdateStatus("Failure" + e.getCause());
e.printStackTrace();
}
return returnValue;
}
I can note that the file is updated in Sharepoint.
I build the response :
Update a document by Id and DriveId
{
"documentName" : "C:\\Users\\me\\Documents\\uploadFolder\\myDoc.docx",
"updateStatus" : "Success",
"updatedAt" : 1590147553641
}
Unfortunatelly, the file can't be opened.
I finally solved the issue by using ByteArrayInputStream...
public UpdateDocumentResponseModel updateDocument(String accessToken, URL url, String fullPath) {
UpdateDocumentResponseModel returnValue = new UpdateDocumentResponseModel();
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(String.valueOf(url));
httpPut.setHeader("Authorization", "Bearer " + accessToken);
httpPut.setHeader("Content-Type", "text/plain");
httpPut.setHeader("Connection", "Keep-Alive");
httpPut.setHeader("Cache-Control", "no-cache");
byte[] fileContent = FileUtils.readFileToByteArray(new File(fullPath));
httpPut.setEntity(new InputStreamEntity(new ByteArrayInputStream(fileContent), fileContent.length));
// httpPut.setEntity(new StringEntity(String.valueOf(in), StandardCharsets.UTF_8));
CloseableHttpResponse response = client.execute(httpPut);
System.out.println("\nSending 'PUT' request to URL : " + url);
System.out.println("Response Code : " + response.getStatusLine());
// set the response
returnValue.setDocumentName(fullPath);
returnValue.setUpdatedAt(new Date());
returnValue.setUpdateStatus("Success");
} catch (IOException e) {
returnValue.setDocumentName(fullPath);
returnValue.setUpdatedAt(new Date());
returnValue.setUpdateStatus("Failure" + e.getCause());
e.printStackTrace();
}
return returnValue;
}
I am using apache httpclient to upload a file to a server. I need to use basic authentication(username and password). The response in 200, however, the server logs reveal that the file in missing some data.
public static String pushdata() throws FileNotFoundException, UnsupportedEncodingException {
File file = new File("/home/mohamed/atomtest.txt");
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pass");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient httpclient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
HttpEntity data = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addBinaryBody("file", file).build();
HttpUriRequest request = RequestBuilder.post("link").setEntity(data).build();
ResponseHandler<String> responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
System.out.println(status);
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
String entityString = EntityUtils.toString(entity);
System.out.println(entityString);
return entityString;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = "pushDATA";
try {
responseBody = httpclient.execute(request, responseHandler);
} catch (IOException ex) {
System.out.println(ex.toString());
}
return responseBody;
}
The file is saved as UTF-16, and must be uploaded with the same encoding.
kind regards.
I didn't find this question on StackOverflow or maybe I searched with loose keywords.
I use to get an InputSteam normally for JSON files on Internet, this is my main function:
static HttpGet getRequest;
static HttpResponse getResponse;
public static InputStream retrieveStream(String url, int timeout) {
getRequest = null;
getResponse = null;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout);
HttpConnectionParams.setSoTimeout(httpParameters, timeout);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
getRequest = new HttpGet(url);
try {
getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ERROR", "Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
} catch (IOException e) {
getRequest.abort();
Log.w("ERROR", "Error for URL " + url, e);
} catch (Exception e) {
getRequest.abort();
Log.w("ERROR", "Error for URL " + url, e);
}
return null;
}
for the scope of avoiding errors or giving the possibility to the user to cancel the request because Internet is ON but isn't working fine, I would like to make a counter when my app is not receiving data and it's not completed. I know how to do the counter, but, how could I make that type of 'listener'?
I use to continue with this (always Asynchronously):
InputStream source = f.retrieveStream(params[0]);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
response = gson.fromJson(reader, Usuario.class);
thanks in advance.
When I execute an API through following method, I always get 404 as response code.
private void execute() throws IllegalStateException, IOException, NoSuchAlgorithmException {
Map<String, String> comment = new HashMap<String, String>();
comment.put("accounts-groups", "customers/enterprise");
comment.put("companyType", "customer");
comment.put("companyName", "Test");
String json = new GsonBuilder().create().toJson(comment, Map.class);
Log.i(TAG, "json : "+json);
HttpResponse response = makeRequest(URL, json);
/*Checking response */
if(response != null) {
InputStream inputStream = response.getEntity().getContent(); //Get the data in the entity
int statusCode = response.getStatusLine().getStatusCode();
Log.i(TAG, "statusCode : "+statusCode);
String result;
// convert inputstream to string
if(inputStream != null)
result = convertStreamToString(inputStream);
else
result = "Did not work!";
Log.i(TAG, "result : "+result);
}
}
private HttpResponse makeRequest(String uri, String json) throws NoSuchAlgorithmException {
Log.i(TAG, "uri : "+uri);
try {
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(json, HTTP.UTF_8));
long timestamp = System.currentTimeMillis();
String signatureKey = PRIVATE_KEY + timestamp;
byte[] bytesOfMessage = signatureKey.getBytes(HTTP.UTF_8);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
char[] signature = Hex.encodeHex(thedigest);
String finalSignature = String.valueOf(signature);
Log.i(TAG, "finalSignature : "+finalSignature);
httpPost.setHeader("Timestamp", ""+timestamp);
httpPost.setHeader("Api_token", API_TOKEN);
httpPost.setHeader("Signature" , finalSignature);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");
return new DefaultHttpClient().execute(httpPost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I am not getting where am I going wrong. Can anybody please help me out?
from wiki:
The 404 or Not Found error message is a HTTP standard response code
indicating that the client was able to communicate with the server,
but the server could not find what was requested.
so, your code is OK, but server cannot find resource you are looking for. Double check if your url is correct.
how to pass request through fiddler proxy for debugging purposes:
HttpParams params = new BasicHttpParams();
// ....
HttpHost proxy = new HttpHost("192.168.1.12", 8888); // IP to your PC with fiddler proxy
params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
// use params as a second parameter to: following constructor:
// public DefaultHttpClient (ClientConnectionManager conman, HttpParams params)
I was getting 404 for POST requests because mod_headers module of Apache 2 server was not enabled. If that happens you can enable it with:
sudo a2enmod headers
and then restart apache:
sudo service apache2 restart
I'm trying to post an attachment o JIRA using the latest REST API.
Here's my code:
public boolean addAttachmentToIssue(String issueKey, String path){
String auth = new
String(org.apache.commons.codec.binary.Base64.encodeBase64((user+":"+pass).getBytes()));
Client client = Client.create();
WebResource webResource = client.resource(baseURL+"issue/"+issueKey+"/attachments");
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
File f = new File(path);
if(f.exists() && f.isFile()){
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
} catch (FileNotFoundException e) {
return false;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
}
fis.close();
bos.close();
} catch (IOException ex) {
try {
fis.close();
bos.close();
} catch (IOException e) {
return false;
}
return false;
}
byte[] bytes = bos.toByteArray();
FormDataBodyPart bodyPart = new FormDataBodyPart("file", new ByteArrayInputStream(bytes), MediaType.APPLICATION_OCTET_STREAM_TYPE);
formDataMultiPart.bodyPart(bodyPart);
}else{
return false;
}
ClientResponse response = null;
response = webResource.header("Authorization", "Basic " + auth).header("X-Atlassian-Token", "nocheck").type(MediaType.MULTIPART_FORM_DATA).accept("application/json").post(ClientResponse.class, formDataMultiPart);
System.out.println(response);
int statusCode = response.getStatus();
System.out.println(statusCode);
String resp = response.getEntity(String.class);
System.out.println(resp);
return true;
}
However, i get the following response:
POST http://localhost:8082/rest/api/2/issue/TEST-2/attachments returned a response status of 404 Not Found
404
XSRF check failed
An Issue with key TEST-2 does exist in the my local JIRA instance and I can add the attachment "by hand" in the Jira app itself.
I know that i must add a header of type "X-Atlassian-Token:nocheck" to prevent XSRF, but, by the output, I must be doing something wrong..
What confuses me even further is that a 404 is thrown after the XSRF check failed.
I've scavenged google for answers with no success
Can anyone hazard a guess to what I'm doing wrong?
I've managed to resolve the issue by using the apache http client
For whom may have the same issue, here's the code:
public boolean addAttachmentToIssue(String issueKey, String path){
String auth = new String(org.apache.commons.codec.binary.Base64.encodeBase64((user+":"+pass).getBytes()));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(baseURL+"issue/"+issueKey+"/attachments");
httppost.setHeader("X-Atlassian-Token", "nocheck");
httppost.setHeader("Authorization", "Basic "+auth);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File fileToUpload = new File(path);
FileBody fileBody = new FileBody(fileToUpload, "application/octet-stream");
entity.addPart("file", fileBody);
httppost.setEntity(entity);
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
return false;
} catch (IOException e) {
return false;
}
HttpEntity result = response.getEntity();
if(response.getStatusLine().getStatusCode() == 200)
return true;
else
return false;
}
#Nuno Neto, I'm surprised your method is working, as it's missing some key elements in the FileBody. Possible update to the Confluence API? Most importantly the file comment, and the encoding. As it were, your example will throw a 500, but for new people coming to this via Google the code below will in fact work.
The major difference here would be:
FileBody fileBody = new FileBody(fileToUpload, fileComment, "application/octet-stream", "UTF-8");
I also have added a small bit of logic for empty file comments.
/**************************************************************************************************
/**
* Confluence integration. This allows the user to attach captured images to confluence pages.
*
/**************************************************************************************************/
/**
*
* #param pageID {int} Page ID of the Confluence page to add to. Navigate to Confluence page, hit 'e', copy the ID from the URI.
* #param {String} path
* #param {String} user Your Confluence username.
* #param {String} pass Your Confluence password.
* #param {String} baseURL Your Confluence url.
* #return {boolean}
*/
public boolean addAttachmentToPage(int pageID, String path, String user, String pass, String baseURL, String fileComment){
String auth = new String(org.apache.commons.codec.binary.Base64.encodeBase64((user+":"+pass).getBytes()));
if ( fileComment.equals("") | fileComment.equals(" ") | fileComment.equals(null)){
fileComment = user + "-" + path;
};
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost( baseURL + "/rest/api/content/" + pageID + "/child/attachment" );
httppost.setHeader("X-Atlassian-Token", "nocheck");
httppost.setHeader("Authorization", "Basic "+auth);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File fileToUpload = new File(path);
FileBody fileBody = new FileBody(fileToUpload, fileComment, "application/octet-stream", "UTF-8");
entity.addPart("file", fileBody);
httppost.setEntity(entity);
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
return false;
} catch (IOException e) {
return false;
}
HttpEntity result = response.getEntity();
// Success!
if(response.getStatusLine().getStatusCode() == 200) {
System.out.println("Confluence -> Exported to the page with ID: " + confPageID);
return true;
}
else {
System.out.println("Confluence -> Error : " + response.getStatusLine().getStatusCode());
System.out.println(response + "\n" + "\n" + response.getAllHeaders() + "\n" + result + "\n" + path + "\n" + "Attempted against: " + baseURL + "/rest/api/content/" + pageID + "/child/attachment" + "\n");
return false;
}
};