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.
Related
I'm trying to call an API with basic authorization but without success.
ClientConfig clientConfig = new ClientConfig();
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("username", "password");
clientConfig.register( feature) ;
clientConfig.register(JacksonFeature.class);
Client client = ClientBuilder.newClient( clientConfig );
String URL = "http://localhost:8080/teste/" + taskId + "/start";
WebTarget webTarget = client.target(URL);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();
System.out.println(response.getStatus());
System.out.println(response.getStatusInfo());
if(response.getStatus() == 200)
{
System.out.println(response.getStatus());
}
I'm getting many errors like:
Could not find a suitable constructor in org.glassfish.jersey.message.internal.SourceProvider$SaxSourceReader class.
java.lang.IllegalArgumentException: Errors were discovered while reifying SystemDescriptor(
Can you help me please?
Some tips?
Greetings
String URL = "http://localhost:8080/teste/" + taskId + "/enable";
HttpPut request = new HttpPut(URL);
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials("user", "password")
);
try (CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.build()) {
try (CloseableHttpResponse response = httpClient.execute(request)) {
// 401 if wrong user/password
System.out.println(response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
if (entity != null) {
// return it as a String
String result = EntityUtils.toString(entity);
System.out.println(result);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I'm using this solution now and its work, but i need to send a raw body in this request, how i can do that?
Any tip?
Thanks
I am trying to upload a file by the POST method with the Apache HttpClient library.
I used the example code for the preemptive basic authentification here:
package ahcs;
// many imports, press ctrl-o in eclipse
public class App {
static final String url = "http://127.0.0.1:64738/test/";
static final String content = "test\nfile\ndata";
static final String httpUser = "testuser";
static final String httpPasswd = "testPassword";
static final String fileUploadFieldName = "uploadData";
static final String fileName = "upload.dat";
public static void main(String[] args) {
System.err.println("Uploading to URL " + url);
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setProtocolVersion(HttpVersion.HTTP_1_1);
MultipartEntityBuilder mpEntityBuilder =
MultipartEntityBuilder.create();
mpEntityBuilder.setMode(HttpMultipartMode.RFC6532);
mpEntityBuilder.addBinaryBody(fileUploadFieldName,
content.getBytes(), ContentType.DEFAULT_BINARY, fileName);
httpPost.setEntity(mpEntityBuilder.build());
System.err.println("executing request " + httpPost.getRequestLine());
HttpEntity resEntity = null;
try {
// Really simple HTTP Authentification, grat Apache
HttpHost httpHost = URIUtils.extractHost(new URI(url));
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(httpUser, httpPasswd));
AuthCache authCache = new BasicAuthCache();
authCache.put(httpHost, new BasicScheme());
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
HttpResponse response = httpclient.execute(httpPost);
resEntity = response.getEntity();
System.err.println(response.getStatusLine().toString());
if (resEntity != null) {
System.err.println(EntityUtils.toString(resEntity));
}
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK) {
throw new HttpResponseException(status,
"Upload error! (" + status + ")");
}
EntityUtils.consume(resEntity);
httpclient.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Unfortunately, it doesn't do what I want. The request what the apache httpclient gives, is this (I got this by listening from the command line with an nc -p 64738 -l command):
POST /test/ HTTP/1.1
Content-Length: 249
Content-Type: multipart/form-data; boundary=PIrvSJ07MLxTV2rC4d-5ZfoL3CvJFJdJqO4i
Host: 127.0.0.1:64738
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.4 (Java/1.8.0_151)
Accept-Encoding: gzip,deflate
--PIrvSJ07MLxTV2rC4d-5ZfoL3CvJFJdJqO4i
Content-Disposition: form-data; name="uploadData"; filename="upload.dat"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
test
file
data
--PIrvSJ07MLxTV2rC4d-5ZfoL3CvJFJdJqO4i--
As we can see, everything is okay, except that the authentification header is simply missing.
Why is it so? What is the bug?
According to RFC7617 you need only one header "Authorization" with values "Basic " + login:passord in Base64 encoding to successefuly pass Basic authorization.
Your code is correct, except one thing - when you call httpPost.execute you are not pass execution context, and AuthCache and CredentialsProvider wasn't used at all.
package ahcs;
// many imports, press ctrl-o in eclipse
public class App {
static final String url = "http://127.0.0.1:64738/test/";
static final String content = "test\nfile\ndata";
static final String httpUser = "testuser";
static final String httpPasswd = "testPassword";
static final String fileUploadFieldName = "uploadData";
static final String fileName = "upload.dat";
public static void main(String[] args) {
System.err.println("Uploading to URL " + url);
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setProtocolVersion(HttpVersion.HTTP_1_1);
MultipartEntityBuilder mpEntityBuilder =
MultipartEntityBuilder.create();
mpEntityBuilder.setMode(HttpMultipartMode.RFC6532);
mpEntityBuilder.addBinaryBody(fileUploadFieldName,
content.getBytes(), ContentType.DEFAULT_BINARY, fileName);
httpPost.setEntity(mpEntityBuilder.build());
System.err.println("executing request " + httpPost.getRequestLine());
HttpEntity resEntity = null;
try {
// Really simple HTTP Authentification, grat Apache
HttpHost httpHost = URIUtils.extractHost(new URI(url));
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(httpUser, httpPasswd));
AuthCache authCache = new BasicAuthCache();
authCache.put(httpHost, new BasicScheme());
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
// context was missed
HttpResponse response = httpclient.execute(httpPost, context);
resEntity = response.getEntity();
System.err.println(response.getStatusLine().toString());
if (resEntity != null) {
System.err.println(EntityUtils.toString(resEntity));
}
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK) {
throw new HttpResponseException(status,
"Upload error! (" + status + ")");
}
EntityUtils.consume(resEntity);
httpclient.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
But for Basic Auth using this API may be a bit verbose, it was designed to support many different authorization schemes.
If you know what charset server will use to decode Authorization header (suppose it UTF-8), you can write one-liner:
httpPost.setHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString((httpUser + ':' + httpPasswd).getBytes("UTF-8")));
I am not certain if I am on the right path. Since I am not getting any data with the HTTP Request. I needed to convert this java code into force.com apex for me to be able to use a WTS web service.
public static HttpEntity getRequestEntity()
{
JSONArray jsonArr = new JSONArray();
HttpEntity entity = null;
JSONObject jsonElement = new JSONObject();
JSONObject jsonObj = new JSONObject();
JSONObject jsonObj1 = new JSONObject();
jsonElement.put("tenant",jsonObj);
jsonObj.put("companyKey",companyName);
String json = jsonElement.toJSONString();
try {
entity = new StringEntity(json);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return entity;
}
private static void GetEnabledUsers(String responseContent) throws ParseException, ClientProtocolException, IOException {
String url = "<endpoint>/services/UserService1.svc/GetEnabledUsers";
// Create an http client to perform the http communication
CloseableHttpClient httpClient = HttpClients.createDefault();
// Http request to specific web service. Request will be given to client
HttpPost request = new HttpPost(url);
// Create credentials for the authentication
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credential = new UsernamePasswordCredentials(companyName+'\\'+userName, password);
provider.setCredentials(AuthScope.ANY, credential);
HttpClientContext localContext = HttpClientContext.create();
localContext.setCredentialsProvider(provider);
// Make the actual http request
HttpPost httpRequest = new HttpPost(url);
HttpEntity requestEntity = getRequestEntity();
if(requestEntity != null) {
httpRequest.setHeader("Content-type", "application/json");
httpRequest.setEntity(requestEntity);
}
CloseableHttpResponse response = httpClient.execute(httpRequest, localContext);
HttpEntity entity = response.getEntity();
String responseContent1 = EntityUtils.toString(entity);
System.out.println(responseContent1);
}
This is what I have so far:
String userName= '<userName>';
String password= '<password>';
String companeyKey = '<companeyKey>';
JSONGenerator jsonGenerator = JSON.createGenerator(true);
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName('tenant');
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField('companeyKey', '<companeyKey>');
jsonGenerator.writeEndObject();
jsonGenerator.writeEndObject();
requestBody = jsonGenerator.getAsString();
Blob headerValue = Blob.valueOf(userName + ':' + password);
HttpRequest requestEntity = new HttpRequest();
String authHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
requestEntity.setMethod('POST');
requestEntity.setEndPoint('<endpoint>/services/UserService1.svc/GetEnabledUsers');
requestEntity.setHeader('Content-Type', 'application/json');
requestEntity.setHeader('Authorization', authHeader);
requestEntity.setBody(requestBody);
Http http = new Http();
HTTPResponse responseEntity = http.send(requestEntity);
system.debug(responseEntity);
On difference seems to be around how the username is constructed for the auth parameter, change
Blob headerValue = Blob.valueOf(userName + ':' + password);
to
Blob headerValue = Blob.valueOf(companyKey + '\\' + userName + ':' + password);
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 have this code, who should connect to a php remote file and should get a String representing a XML file. But something is wrong, it is giving me error 401.
The variable url is the direction of the php:
String response=getXML("http://ficticiousweb.com/scripts/getMagazinesList.php");
If i paste the real direction (that is a ficticious direction) on the webbrowser, it works and gives me the XML.
This is my code:
public String getXML(String url){
try{
StringBuilder builder = new StringBuilder();
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
int statuscode = response.getStatusLine().getStatusCode();
if(statuscode == 200)
{
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) builder.append(line);
}
else throw new Exception("HTTP error: " + String.valueOf(statuscode));
return builder.toString();
}catch(Exception e){e.printStackTrace();}
return null;
}
What is wrong with the code?
thanks
You need to login to the requested site in order to download or access the xml. This can be done by authenticated schema based upon what is supported. Normally, there are 2 types of schemas where used. Basic and Digest. Below code will help you for BASIC AUTH.
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String _username = "username";
String _password = "password";
try {
((AbstractHttpClient) httpclient).getCredentialsProvider().setCredentials(
new org.apache.http.auth.AuthScope(webhostname, webport)),
new org.apache.http.auth.UsernamePasswordCredentials(_username, _password));
response = httpclient.execute(new HttpGet(completeurlhere));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK) {
try {
InputStream is = response.getEntity().getContent();
this._data = is;
} catch(Exception ex) {
Log.e("DBF Error",ex.toString());
}
} else {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch(ClientProtocolException cpe) {
Log.e("ClientProtocolException # at FPT",cpe.toString());
} catch(Exception ex) {
Log.e("Exception at FETCHPROJECTASK",ex.toString());
}
Well a 401 means you aren't Authorized to do the GET request. You should ask the website how to Authenticate the request...
Authorization happens through the Authorization Header in the HTTP request. You should look into that and probably fill that header yourself with your credentials... (if the server accepts that)