How to put parameters in URL using Java and httpclient? - java

I am really new to Http stuffs and am trying to put parameters in the URL in order to make some requests from a certain API.
For example, the guide for the API says that we need to do
https POST api_site.io/users/$username/token,
but using Java, how do we pass the username parameter before /token and do a request for POST?
This is the start and I have a lot more things to do for the next but if someone enlightens the way of how to start, I think can do the other things by myself.
Thanks in advance!

Related

Spring SAML and internal post

I am implementing a SAML authentication in a Spring project, with the spring-security-saml plugin and I achieved SSO without problems. Now I need to migrate a function in which we do a POST to another servlet in my app, where we previously managed the security with HTTP basic. We used org.apache.commons.httpclient.HttpClient library, in this way:
HttpState hs = new HttpState();
AuthScope autScope = new AuthScope("host", "port");
hs.setCredentials(autScope, credentials);
Now, since we do not have the password, I was wondering which approach should be used to fix that. I am getting a 302 response since it tries -logically- to redirect to my SSO page.
I read extensively the documentation of SAML but is difficult to me to understand where is the key. Maybe some work with SAML Assertion? Please give me some directions so I could read in a more precise way.
Thanks in advance!
Do I understand correctly that both the calling code and the POST endpoint are part of the same application? If so, I'm not sure why are you using an HTTP call instead of passing the call internally within the application.
In any case, one way to make it work is by sending JSESSIONID cookie of the authenticated user together with the POST call.

Simulate form post using http client in Android app?

So, I'm currently developing an app for a service which has a json-based (unfortunately) read only API. Retrieving content is no problem at all, however the only way to post content is using a form on their site which location is a PHP script. The service is open source so I know which fields the form expects, but whatever I send, it always results in a BAD REQUEST.
I captured the network traffic inside my browser and as far as I can see, the browser constructs a multipart form request, however when I copy the request and invoke it again using a REST client, a BAD REQUEST gets returned.
Is there a way to construct a http request in Android that simulates a form post?
If it's readonly I think you wouldn't be able to make requests with POST (it's assume for editing or adding things).
If you let me make you an advise, I recommend you using this project as a Library.
https://github.com/matessoftwaresolutions/AndroidHttpRestService
It makes you easy deal with apis, control network problems etc.
You can find a sample of use there.
You only have to:
Build your URL
Tell the component to execute in POST mode
Build your JSON
As I told you, I don't know even if it will work.
I hope it helps!!!

Spring MVC forward to another URL

I create a web-application with Spring 4. It must work as proxy for internal resources of our company.
When it receives user requests, it analyses its correctness and user privilegies, and if everything is correct, gives user the result.
So, is it possible to do forward request to page like http://xxxxx.com:8983/solr?
If yes, show the example, please
So that user writes url of my application and sees the page http://xxxxx.com:8983/solr
P.S. I tried to find it in google, but everywhere were answers about redirect but not forward
Well, I think there are multiple ways of dealing with this. But my gut feeling is that you're going to have to write some code yourself.
Basically, I would use HttpClient to make a request to your proxied website, take the input stream from the httpclient connection and stream it to the output stream of your spring application response.
You could handle all this interaction in the controller itself, but I think using a specialized ViewResolver might be better.
Have you tried:
return "forward:/page/section/";

Can I do an http GET and include a body using HttpURLConnection?

I need to perform an http GET for a REST service and include a body in the GET. Unfortunately, setting #setDoOutput( true ) on the connection forces a POST. Is there anyway to send a GET with a body?
Edit: The body I'm trying to send is JSON.
It is not possible to send content for an HTTP GET using HttpURLConnection. By setting setDoOutput(true) on an HttpURLConnection the verb is forced to be POST.
The documentation for the REST API I was using described a JSON body for the endpoint in question, but URL parameters were accepted.
It might not be possible through HttpUrlConnection, although you might be able to do it through another APIs BUT, if you have to do it that way chances that you are doing something wrong in your architecture are high because it goes against the basic usage of GET Http Method and different problems might arise like:
If you ever try to take advantage of caching, Proxies are not going to look in the GET body to see if the parameters have an impact on the response.
It's not a good implementation based on standard practices and it could cause problems with some browsers / services.
Take a look at this question for more information.
HTTP GET with request body
Hope this helps.
Regards!

Possible to Authenticate with an website with POST / Download CAPTCHA

I've often wanted to create applications that provide a simpler front-end to other websites that require users to login before the pages I want to use can be accessed. I was wondering, if
(1) any website with a POST to an http page can be authenticated by POSTing
postField1name=pf1Value&postField2name=pf2Value
to the website, if that's true how can you inspect the HTML to POST correctly?
(2) I wanted to know if you could parse HTML, say for a sign up form, and display all the fields in an application UI, including downloading a Captcha, and displaying it to the user, and allowing them to type the value in, to send back to the website, and process the response.
Also if anyone knows how I might accomplish (2) using Apache HTTP Client in java, I'd greatly appreciate it!
http://hc.apache.org/httpcomponents-client/httpclient/index.html
(1) An easy way to find out what's actually being POST'd is to look at the actual HTTP requests. You can do that with a tool like LiveHTTPHeaders. Then have your script simulate that.
(2) Yes. You can use cURL, which is excellent for things like this.
(1) Try FireBug. There's actually a lot of options for authentication.
(2) Try JTidy

Categories

Resources