I used to have this code working with my Tomcat server:
HttpRequestBase targetRequest = ...;
HttpResponse targetResponse = httpclient.execute(targetRequest);
HttpEntity entity = targetResponse.getEntity();
However when I migrated with Google App Engine, I can' use this code anymore. So I read a bit and found that I need to use another code to achieve this.
So I have this code:
URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
HTTPResponse targetRespose = fetcher.fetch(targetRequest); // Error
HttpEntity entity = targetResponse.getEntity();
However its obvious that there's an error with the fetcher.fetch code.
All I need to accomplish to to have the same HttpEntity using App Engine approach. Any way to work this out?
org.apache.http.HttpRequest and com.google.appengine.api.urlfetch.HTTPRequest are two totally different classes from two different libraries, so you can not just exchange one for the other.
If you'd like to use Apache HttpClient on GAE, it can be done with some workarounds: see here and here.
Related
I have some code to download a file utilizing org.apache.http.client.HttpClient. Now my IDE tells me that I have a Potential resource leak: 'client' may not be closed. The concrete code is:
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
I did some research and found EntityUtils.consume(entity);, but this doesn't solve the resource leak for client.
So my question is, is this really a resource leak and if yes, how do I close it properly?
As I couldn't find any other way and Eclipse IDE didn't have any other quick-fix available, I tried the only proposed "fix" which was to merge all the 3 lines into 1 line like this:
HttpEntity entity = HttpClientBuilder.create().build().execute(request).getEntity();
I am not sure if this actually solves the resource-leak problem, but at least Eclipse seems to think to
I'm trying to make a little utility that will synchronise data between two servers. Most of the calls there are REST calls with JSON, so I decided to use Apache HttpClient for this.
There is however a section where I need to upload a file. I'm trying to do this using the mutipart form data with the MutipartEntityBuilder but I encounter a Content too long problem. (I tried to gzip the contents of the file too, but I'm still going over the limit).
Here's my java code:
HttpPost request = new HttpPost(baseUrl+URL);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//create upload file params
builder.addTextBody("scanName", "Test upload");
builder.addBinaryBody("myfile", f);
HttpEntity params= builder.build();
request.setEntity(params);
request.addHeader("content-type","multipart/form-data");
HttpResponse response = httpClient.execute(request);
Are there better atlernatives that I should be using for the file upload part? I'm also going to download the files from one of the server. Will I hit a similar issue when try to handle those responses?
Is there something I'm doing wrong?
I try to use your code and send some file with size something about 33MB and it was successful. So, I think your problem one of the follows:
Created http client has limitations for request size - in this case you need to change properties of client or use another client;
In some peace of code you call HttpEntity.getContent() method. For multipart request for this method exists limitations - 25kB. For this case you need to use writeTo(OutputStream) instead of getContent()
In comments you told about swagger, but I don't understand what does it mean. If you use swagger generated api, that problems maybe occurred at their code and you need to fix generation logic (or something like this - I never used swagger)
I hope my answer will help you
How would I go about writing a program that can take articles from Google News and download them to my computer?
I've found that Google News already has a built in RSS feature, but I need to actually download the entire article (text and all) rather than just a headline.
Preferably, I'd like to download these articles as PDFs or HTML files, but for starters just fetching some URLs would be amazing.
There have been some questions on here about fetching articles from Google News, but nothing I've found so far has been particular helpful. Any help would be massively appreciated.
Thanks!
Legal issues aside, this is possible, see Apache HttpComponents. Here is an example (taken from here) of how to use it:
DefaultHttpClient httpclient = new DefaultHttpClient();
if ( useProxy == true ) {
HttpHost proxy = new HttpHost(proxyStr, 80, "http");
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
HttpGet httpget = new HttpGet(urlStr);
httpget.addHeader("Authorization", "Basic " + encodedAuth);
HttpResponse response = httpclient.execute(httpget);
But be aware of Google TOS before you do anything like this.
I have a web service that I built... what I am trying to do now is send a simple request that contains a json query string from a Tapestry web app to that web service. I searched around and most people say to use Apache HttpClient to achieve this. Along with HttpClient I am using URIBuilder.
The Json object that I am trying to send looks like this
{"user":{"userEmail":"jdoe#gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"}
*I realize the issues with the password being sent in plain text etc...
The url that works (tested by manually entering in a web browser and this web service already services an Android client and an iOS client) looks like this
http:// ##.##.###.##/createuser?json={"user":{"userEmail":"jdoe#gmail.com","firstName":"John","lastName":"Doe","phone":"203-555-5555"},"password":"dead"}
Here is the HttpClient code that I have mashed together from google'ing around trying to figure out why this wont work. Essentially what I am trying to do is create a URI with URIBuilder and then construct an HttpPost or HttpGet object with the newly built URI. But something is going wrong in the URIBuilding process. When I debug, an exception gets thrown when I try to set all the aspects of the URI.
Object onSuccess() throws ClientProtocolException, IOException, URISyntaxException{
// json = {"user":{"userEmail":"jdoe#gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"}
String json = user.toJson();
URIBuilder builder = new URIBuilder();
// Error gets thrown when I step over the next line
builder.setScheme("http").setHost("##.###.##.###").setPort(8080).setPath("createuser").setQuery("json=" +json);
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
DefaultHttpClient httpClient = new DefaultHttpClient();
String tmp = request.getURI().toString();
HttpResponse response = httpClient.execute(request);
index.setResponse(EntityUtils.toString(response.getEntity()));
return index;
The error that comes back when I step over the line that I commented in the code is
[ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/util/List;
java.lang.NoSuchMethodError:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/util/List;
I have tried a lot of other combinations of methods and objects to get this request to send off to the server correctly and nothing seems to work. Hopefully I am overlooking something relatively simple.
Thanks in advance for any guidance you can provide.
You most likely have the wrong version or two versions of the apache httpcomponents on your classpath. If you are running Tapestry it will print out all packages on the classpath on the error page. Investigate there, find which httpcomponents is loaded, figure out where it comes from and fix it.
If this does not work, you should share some of your runtime environment with us. Which servlet engine, running from which IDE or are you running from the command line. Are you using Maven? If so share your pom. Etc.
I've been trying to find a way to upload a video from an Android device to an API, but I haven't found a good way to do it. It seems most of the information I've found online is fairly out of date (a lot of it being from last year). Most of them are using a method like this: http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html
What's the easiest/preferred way to upload something to an API with a multipart POST?
I have an Android app I'm developing against the Campfire chat service's "API". The code here uploads a file through multipart POST:
http://github.com/klondike/android-campfire/blob/master/src/com/github/klondike/java/campfire/Room.java#L175
Everything after the "dos.close()" line is related to checking the response to detect whether the post was successful.
Not everything in there is necessary for every multi-part post; for example, the X-Requested-With header is specific to Campfire, the User-Agent is optional, and the Cookie is because I have to stay logged in. Also, the "OH MY GOD" comment about spacing is probably Campfire-specific.
I've heard that the latest version of the HttpClient library from Apache has more convenient built-in multi-part support, but the last sync Google performed against it to Android didn't include those features, so here I am doing it manually.
Hope that's of some help.
You could use the HttpClient from the Apache Software Foundation. It is part of the Android API:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.somewebpage.com/site-that-can-handle-post");
try {
MultipartEntity entity = new MultipartEntity();
entity.addPart("timestamp", new StringBody("1311789946"));
entity.addPart("image", new FileBody(new File("/foo/bar/video.mpeg")));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
Log.v(MyActivity.TAG, "doh!", e);
}
Hope that helps. :)