I have problems getting the HTML code of a website by using this example code.
http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java
I have to use the above one, as I need preemptive authentication for my application in Android.
I thought the solution might be the httpget object, but I still get only errors :(
c.f. HttpGet httpget = new HttpGet("/index.html");
Does anybody have an idea how to get the content of the file, which is specified in the httpget object above using the example code of the link. It definitely called, but for now I can only get status code and so on ...
Thanks 4 help
When I tackled this last year, I gave up on HttpClient's native pre-emptive HTTP authentication and just rolled the header myself.
Alternative 1: Please read Http Basic Authentication with Android that proposes a solution based on the HttpClient 4 official docs. I've not tested it by myself, so I'd be happy to know if it really works.
Edit: I've just tried it and it works like a charm.
Alternative 2:
You can also add the "Authorization" HTTP header as proposed by #CommonsWare:
post.addHeader("Authorization", "Basic " + Base64.encode(username+":"+password));
In this case you need a Base64 encoder to encode the string containing the username and the password. You can find a lot of implementations in the Internet.
For me the example above didn't work on Android. I had to do the following:
post.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.NO_WRAP));
Thanks janex.
I had to do the same on Android.
post.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.NO_WRAP));
cheers
Related
I'm trying to make an Http GET and send JSON as input:
https://host/X/Y?input={"foo":"bar","baz":"yak"}
according to these posts:
HttpGet with request body android
Apache's HttpGet (used by Android) does not support this kind of thing.
I've tried some of the alternatives specified in that post and they haven't worked.
First, can someone confirm Apache's HttpGet won't let you send JSON.
Next, can someone suggest any ways around this - alternate libraries, other classes, anything ….
put some json into a file and this example will post it
dont know why a GET would be used with much json info?
Figure out the 'entities' implementation along with POST and choose a better httpclient than you are using.
When submitting JSON, you might prefer using POST instead if GET. In case you do want to use GET, don't forget to encode your URL with URLEncoder.encode(json, "UTF-8"). This might be the cause of your problem.
I am having the following API call:
http://rollout.gr/api/?query={%22search%22:%22places%22,%22page%22:1}
This API call is executed correctly in my browser. But when I use a DefaultHttpClient to execute this url in my Android application, I get a null response.
I suppose the problem is the JSON data in the HTTP url. Thus, I would like to ask which is the proper way to handle such url in an Android application?
Thanks a lot in advance!
The accolades aren't valid URL characters. The browser is userfriendly enough to automatically URL-encode them, but DefaultHttpClient isn't. The correct line to use from code is:
http://rollout.gr/api/?query=http://rollout.gr/api/?query=%7b%22search%22:%22places%22,%22page%22:1%7d
Note the encoding for the accolades (%7b, %7d).
Your problem may be the strictmode here.
I recommend to do http request in threads or asynctasks. strictmode doesnt let app do http reauest in uithread. maybe your console shows a warning and you get null from http response because of this.
This project may solve your problem:
http://loopj.com/android-async-http/
Not knowing your particular HTTP initialization code, I'm going to assume you didn't provide an explicit JSON accept header. A lot of REST endpoints require this.
httpget.setHeader("Accept", "application/json");
I am getting an error in the process of OAuth in evernote SDK sample HelloEDAM.
org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this
Is there any way to get rid off this problem?
The exception should include the actual response body:
throw new OAuthException("Response body is incorrect. Can't extract token and secret from this: '" + response + "'", null);
Can you add that so we can see what you're getting back?
Maybe a bit late... but I'll answer for future visitants.
Probably you didn't change the EVERNOTE_CONSUMER_KEY and the EVERNOTE_CONSUMER_SECRET by default.
You must to ask for an API key at the Evernote for Developers webpage:
https://dev.evernote.com/
You should add that in your gradle.properties, and follow the instructions at
https://github.com/evernote/evernote-sdk-android
Disclaimer: At least, I'm getting the same exception today with the latest Evernote SDK, I don't know how it was three years ago.
I'm running into a strange problem using HttpClient. I am using a DefaultHttpClient() with HttpPost. I was using HttpGet with 100% success but now trying to switch to HttpPost as the REST API I'm using wants POST parameters rather than GET. (Only for some API calls though so I know that the GET calls were working fine so it's not a fault of the API).
Also, I tried using HttpPost on a simple php script I wrote that looks for a POST parameter 'var' and echoes it to screen, passing this parameters as follows worked fine:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
postMethod = new HttpPost("http://www.examplewebsite.com");
nameValuePairs.add(new BasicNameValuePair("var", "lol"));
try {
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(postMethod, responseHandler);
Log.i("RESTMethod", response);
...
The problem is that when I tried and do the same call to the API (but with the params changed to the API params obviously) I get the following error:
Authentication error: Unable to respond to any of these challenges: {}
The page I am requesting is an HTTPS page, could this be the problem?
But doing the same type of POST request to a raw HTTP page on the API gives the same error, unless I comment out the StringEntity part and then it runs (but returns xml and I want to pass a parameter to request the data in JSON).
This seems like a really strange problem (the non-https part) but couldn't really find any help on this problem so sorry if the answer is out there.
Any ideas?
Thanks in advance,
Infinitifzz
EDIT: Okay I'm getting nowhere so I thought if I directed you to the API it might shed some light, it's the 8Tracks API and as you can see you need to pass a dev key (api_key) for all requests and I the part I'm stuck on is using https to log a user in with: http://www.8tracks.com/sessions.xml" part.
Hope this helps somehow because I am at a dead end.
Thanks,
Infinitifizz
Authentication error: Unable to
respond to any of these challenges: {}
This error message means that the server responded with 401 (Unauthorized) status code but failed to provide a single auth challenge (WWW-Authenticate header) thus making it impossible for HttpClient to automatically recover from the authentication failure.
Most likely application expects some soft of credentials in the HTML form enclosed in the HTTP POST request.
Don't you have to declare the port and protocol? I'm just swagging this code so please don't be upset if it doesn't immediatley compile correctly. Also, I usually supply a UsernamePasswordCredentials to my setCredentials() but I imagine it's the same.
HttpHost host = new HttpHost("www.foo.com", 443, "https");
// assemble your GET or POST
client.getCredentialsProvider().setCredentials(new AuthScope(host.getHostName(), host.getPort()));
HttpResponse response = client.execute(host, [HttpPost or HttpGet]);
More info about setCredentials here.
Here's how I ended up with similar problem:
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(username, password));
Thanks to Ryan for right direction.
Not specifying a Callback URL for my Twitter App resulted in the same error for me:
Authentication error: Unable to respond to any of these challenges: {oauth=WWW-Authenticate: OAuth realm="https://api.twitter.com"}
Setting a callback URL on Twitter fixed the problem
I'd like to know how to do a HTTP GET request to a server from which I know I will get redirected at least a couple of times...
I'd like to get the response (body) of the "last" webpage. It should come up with a code in my specific example...
Or, as an alternative, I do know you can start the browser from within your Android app, is it possible to actually retrieve the body from that ?
Any help, tips, source whatever would be helpfull, cause till now I have found (and tried) like 4 methods and to me it seems there is a jungle of HttpGet, HttpClient, HttpResponse etc libraries ?
final HttpParams params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, false);
You can see more on redirect support with HttpClient in other StackOverflow answers.
is it possible to actually retrieve
the body from that ?
No, that would be a security violation.