I am getting a HTTP response code 405 when I use HttpURLConnection to make a DELETE request:
public void makeDeleteRequest(String objtype,String objkey)
{
URL url=null;
String uri="http://localhost:8180/GoogleMapsLoadingTest/rest/GoogleMapsErp/";
HttpURLConnection conn=null;
try {
url=new URL(uri);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn=(HttpURLConnection) url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn.setDoInput(true);
conn.setDoOutput(true);
try {
System.out.println(conn.getResponseCode());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
conn.setRequestMethod("DELETE");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
How can I make this request?
The 405 status code means that your method (DELETE) is not allowed for the resource you specified; in this case, what looks like an entire REST endpoint directory. You should use DELETE on the specific item you want deleted; perhaps you forgot to actually use the method parameters when constructing the URL?
It would appear to be a problem with your server. I'm guessing nginx, but let me know if not.
Take a look here to see how to compile ngninx with HttpDavModule. There are other servers that have issues with this, but nginx doesn't have it by default.
If you're running Apache, check your module configurations to see if you're disallowing them or not. Here's a post about a previous solution. Unfortunately, this problem is typically specific to modules that have been installed. On a vanilla installation, however, you can often just allow for DELETE (see your config file, as well as the OP of that link)
Related
I'm trying to get information from a Html page using WebClient but I don't manage to get the correct page. It's not waiting for the query to be finished.
Anyone has an idea? It must be linked to the website I'm trying to read. For others website my code is working
Here an extract of the code I use to get the Html page
public void initWebClient() {
WebClient _webClient = new WebClient(BrowserVersion.FIREFOX_10);
_webClient.getOptions().setJavaScriptEnabled(true);
_webClient.getOptions().setThrowExceptionOnScriptError(false);
_webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
_webClient.waitForBackgroundJavaScript(10000);
_webClient.waitForBackgroundJavaScriptStartingBefore(10000);
_webClient.setCssEnabled(false);
try {
HtmlPage _pageRecherche = (HtmlPage) _webClient.getPage("https://www.blablacar.fr/trajets/paris/lyon/#?fn=PAris&fc=48.856614%7C2.352222&fcc=FR&fp=0&tn=LYON&tc=45.764043%7C4.835659&tcc=FR&tp=0&db=25%2F05%2F2017&sort=trip_date&order=asc&limit=10&page=1");
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thanks
Joe
Use this code block inside the try-catch block not outside. If this fails check with another OS.
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
HtmlPage page = (HtmlPage) webClient.getPage("https://stackoverflow.com/users/login");
System.out.println("Yeyy! :) Loaded Page!");
Previously I was using the solrj 4.9.1 for connecting with solrserver 4.9.1.
Now I have upgraded it to solrj 5.4.1 to connect with solrserver 5.4.1. But the problem is that previously the code,
QueryResponse res = null;
try {
res=solrServer.query(query);
} catch (SolrServerException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
used to work properly. But now in solrj 5.4.1 it is throwing an IO Exception for processing the same data. When I have changed the code as,
QueryResponse res = null;
try {
solrServer.query(query);
} catch (SolrServerException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
it is passing through. Although I am not getting any result set. But there is no error during the query. So what I think is that there is some problem or change in the QueryResponse constructor. But I have not found any change in the solrj documentation.
Both in 5.4.1 as well as in 4.9.1
https://lucene.apache.org/solr/5_4_1/solr-solrj/org/apache/solr/client/solrj/response/QueryResponse.html
https://lucene.apache.org/solr/4_9_1/solr-solrj/org/apache/solr/client/solrj/response/QueryResponse.html
only that SolrServer is changed to SolrClient. Is there really any change? or I am doing it wrongly?
I am developing an android app that reads a webpage using AsyncTask.I have 2 AsyncTask classes that reads different page, one inside service, other not.In both cases url connection method is enclosed inside try-catch block , but their catch blocks are different(TimeoutException catch block missing in the 2nd AsyncTask class, tried to catch or throw it manually but failed) .Even though their connection methods are same, their try-catch blocks are different, but I need to catch TimeoutException in both task.
Here is part of my 1st AsyncTask class
try {
url=new URL(link);
huc=(HttpURLConnection) url.openConnection();
huc.setRequestMethod("GET");
huc.setDoOutput(true);
huc.setReadTimeout(READ_TIMEOUT);
huc.connect();
dothework();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
showErrorMsg();
e.printStackTrace();
}
and part of 2nd AsyncTask class
try {
url = new URL(link);
HttpURLConnection huc=(HttpURLConnection) url.openConnection();
huc.setReadTimeout(5000);
huc.setDoOutput(true);
huc.setRequestMethod("GET");
huc.connect();
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
I'm not sure, but I think a HttpConnection is a AsyncTask already. So, I think you're making a call to an AsyncTask inside another.
I have the following copyfile.php file inside my main directory with the below code :
<?php
copy("dir1/test.php","dir2/test.php");
?>
Basically just moving a file from dir1 to dir2 (both the directories are already created and are present in my main directory)
I am using the following java code to call my copyfile.php
try {
URL url;
url = new URL( "http://www.xyz.com/copyfile.php" );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){
InputStream is = conn.getInputStream();
// do something with the data here
}else{
InputStream err = conn.getErrorStream();
// err may have useful information.. but could be null see javadocs for more information
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
The program runs without any errors but the file is not being copied.
But if I paste the above url (http://www.xyz.com/copyfile.php) in the browser it works fine the file gets copied to dir2. Am I doing something wrong? Please help. Thanks in advance :)
I'm trying to write to a text file on my web server using HttpURLConnection.getOutputStream(). I have tried this on two different servers without success.
I have added a FileWriter to test the InputStream, and that file is created on a local directory correctly, but nothing is showing up on the in the web server directory, even with all password protection off.
Any help would be greatly appreciated.
URL url;
try {
url = new URL("http://www.myWebsite.com/myFile.txt");
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
OutputStream in = new BufferedOutputStream(urlConnection.getOutputStream());
InputStream fin1;
try {
fin1 = new FileInputStream(Environment.getExternalStorageDirectory() + "/fileToRead.txt");
FileWriter fWriter = new FileWriter(Environment.getExternalStorageDirectory() + "/fileToWrite.txt");
int data = fin1.read();
while(data != -1) {
fWriter.write(data);
in.write(data);
data = fin1.read();
}
fWriter.flush();
fWriter.close();
fin1.close();
in.flush();
in.close();
} catch (FileNotFoundException e31) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
urlConnection.disconnect();
}
} catch (IOException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
} catch (MalformedURLException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
You have to call getInputStream() on the urlConnection in order to get the output stream to flush out the socket to the remote server.
See the discussion here: Why do you have to call URLConnection#getInputStream to be able to write out to URLConnection#getOutputStream?
You are catching the IOException (Right after the IOException) but are not doing anything with it. At least print the stack trace.
You can also use Apache's Http Client http://hc.apache.org/httpcomponents-client-ga/index.html
Much easier than getting URLConnection to work.