Guys I may or may not be wrong, But seriously, I am struggling with file uploading problem in Amazon S3 bucket. When I am trying to hit on the request then I am getting the following error.
MethodNotAllowed and The specified method is not allowed against this resource
The above message is the sort of the below response.
<?xml version="1.0" encoding="UTF-8"?><Error><Code>MethodNotAllowed</Code
<Message>Thespecified method is not allowed against this resource.</Message>
<Method>POST</Method><ResourceType>OBJECT</ResourceType>
<RequestId>xxx</RequestId><HostId>xxxx</HostId></Error>
The above message is the complete message and below is the code whatever I have written for uploading files to amazon s3 server.
public synchronized boolean uploadFile(String url, String name, File file) {
HttpEntity entity = MultipartEntityBuilder.create()
.addPart("file", new FileBody(file)).build();
HttpPost request = new HttpPost(url);
request.setEntity(entity);
HttpClient client = HttpClientBuilder.create().build();
try {
HttpResponse response = client.execute(request);
entity = response.getEntity();
if (entity != null) {
try (InputStream in_stream = entity.getContent()) {
BufferedReader in = new BufferedReader(new InputStreamReader(in_stream));
String inputLine;
StringBuilder responseBuffer = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
responseBuffer.append(inputLine);
}
in.close();
String a = responseBuffer.toString();
Utils.print("\n\n" + a + "\n\n");
}
}
return true;
} catch (Exception e) {
Utils.print(e);
}
return false;
}
Please suggest me what to do for this? I will be very thankful for your expected answer.
You could be getting MethodNotAllowed, which recommends using an identity that belongs to the bucket owner's account.
If you have the correct permissions, but you're not using an identity that belongs to the bucket owner's account, Amazon S3 returns a 405 Method Not Allowed error.
https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketPolicy.html
from a list of S3 API error responses
The specified method is not allowed against this resource.
https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html
Related
In my java code I'm calling third pary apis. In my local workspace it works pretty well and I'm getting proper response. But When I deploy the war in production, I see in logs, I'm calling the apis and didn't get any response. It sound wierd to me and got stuck with it. Please find the snippet and I'm completely calling api in plain old Java.
private void updateRetailerOrderStatus(String trackingId) {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
System.out.println("Inside updateRetailerOrderStatus By Android");
Order order = orderDao.getOrdersByTrackingId(trackingId);
HttpPost httpPost = new HttpPost("http://SomeDomine/v1/order/delivered");
httpPost.addHeader("Some Key", "Some Value");
httpPost.addHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("awb_no", order.getTrackingId());
json.put("order_no", order.getOrderId());
json.put("delivered_date", order.getDeliveredDate().toString());
json.put("status", "delivered");
json.put("carrier", "Delivery");
StringEntity entity = new StringEntity(json.toString());
httpPost.setEntity(entity);
System.out.println(json.toString());
HttpResponse response = httpclient.execute(httpPost);
BufferedReader buffReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer respBuffer = new StringBuffer();
String line = "";
while ((line = buffReader.readLine()) != null) {
System.out.println(line);
respBuffer.append(line);
}
System.out.println("API Response: "+ respBuffer.toString());
} catch(Exception ex) {
System.out.println("Error in fetchToken :"+ ex.getMessage());
System.out.println(ex.getStackTrace());
} finally {
try {
httpclient.close();
} catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
My Questions are:
1) Is there a possibility of happening this? If So, Why?
2) Should I call this in saparate thread? But, I didn't see any suggestions of doing it in most of the examples.
I'm trying to remotely deploy application to Tomcat. To do that, I need to do the following GET request:
http://localhost:8080/manager/text/deploy?path=/client-001&war=file:C:/.DS/tmp/client-001.war
I do it from my Java code:
String url = "http://localhost:8080/manager/text/deploy?path=/client-001&war=file:C:/.DS/tmp/client-001.war";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request;
try {
request = new HttpGet(url);
request.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("test", "test"),
"UTF-8", false));
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.err.println(result.toString());
} catch (Exception e){
e.printStackTrace();
}
but I get 403, even though I've passed my credentials.
What am I doing wrong?
So I found out what the problem was.
1) I didn't need to pass credentials to the Header, I just needed to change url from localhost:8080 to test:test#localhost:8080
2) My user test had role manager-gui, and for GET to work it needed the role manager-script
I'm trying to understand how can I link by Android App to PHP/MySQL in order to get a login from a user that is already on a database.
I found that one way of doing it was through going to PHP pages, sending the inputed data from the app and post it on parameters on PHP, and PHP itself should look for the user references and return if the user either exists or not.
However, every place I look for how to do this it is too much either complicated or not well explained.
So I'm looking for a way to do this, and if you know some tutorial or well commented code that you could provide for me to learn it would be very useful to me. Or any of you could teach me how to do it or where to start learning would be great.
Thank you very much in advance.
For this kind of interaction I use this code:
public static String httpPost(String address, List<NameValuePair> parameters) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(address);
InputStream inputStream = null;
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
httpPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
reader = new BufferedReader((new InputStreamReader(inputStream)));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (reader != null)
reader.close();
if (inputStream != null)
inputStream.close();
}catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
You have to pass this function a String which is the URL of the PHP script, and a list of NameValuePairs containing the data you want to pass to the script, and it returns the answer from the server.
If you want to use it for login, you can use something like that:
public boolean login(String username, String passwd) {
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", username);
parameters.add(new BasicNameValuePair("password", passwd);
String result = httpPost("http://test.com/login.php", parameters);
return result.equals("ok");
}
There are already answers mentioning about posting Input data to your PHP web page. How ever you can also follow another approach.
In this approach you will need a webview loaded initially(assuming login is needed for all activities). In the web view simply load the web page, say http://www.example.com/login?device=mobile Once user has logged in successfully, assuming that website would load a logged in page, example.com/user you need to check if this was the URL which is opened. Then it means that login was completed successfully. Then you could proceed to next activity in your App.
Reference
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)
I'm attempting to receive a response from a restful service, but receive a timeout. I am able to connect with the browser on my emulator, as I have configured an access point on the emulated device to pass through proxy (at work). Network seems to be fine. I've added:
<uses-permission android:name="android.permission.INTERNET" />
to the AndroidManifest.xml file.
The code is as follows:
public String getInputStreamFromUrl(String url) {
String content = null;
InputStream stream = null;
try {
HttpGet httpGet = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
// Execute HTTP Get Request
HttpResponse response = httpclient.execute(httpGet);
stream = response.getEntity().getContent();
BufferedReader rd = new BufferedReader(new InputStreamReader(stream), 4096);
String line;
StringBuilder sb = new StringBuilder();
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
content = sb.toString();
} catch (Exception e) {
content = e.getMessage();
}
return content;
I know I should return a stream, but for the sake of just displaying some string values in a TextView widget, will suffice, so I'm just using the string to experiment. It consistently hangs on .execute, no matter what URL is passed. I've passed valid IP's as well, with nothin' doin'.
I appreciate your help in advance.
Try this. Put it at the top of the class.
System.setProperty("http.proxyHost", <your proxy host name>);
System.setProperty("http.proxyPort", <your proxy port>);