| Author |
Send a query to the server from my Java Class
|
Angus Ferguson
Ranch Hand
Joined: Jun 22, 2012
Posts: 246
|
|
Hi,
From my Java class(it is not a servlet), I want send a request like this to my server
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code
I want send it to the server in order to retrieve an answer.
Do I need set it in the request and send it, send it like a String?
I am very interested in make it work. Do you have any idea?
Many Thanks,
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12271
|
|
It is best to let the Java library handle the request headers, but the body of the request can just be a string. Here is an example:
There are also toolkits such as the Apache Software Foundation HttpClient.
Bill
|
Java Resources at www.wbrogden.com
|
 |
Angus Ferguson
Ranch Hand
Joined: Jun 22, 2012
Posts: 246
|
|
Hi Bill,
many thanks for reply.
I have read the code and I have some doubts. For example:
What the variable action means? Is i the request?
Is serviceURL the formal URL without parameters something like www.angus.com
Could I use GET as method?
Why this code is needed:
while( ch != -1 )
{
sb.append((char)ch);
ch = read.read();
Also I have checked that "reqT" is not the request because it doesn't contain the method getRequest(), then what is it?
}
Regards,
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12271
|
|
I just cut and pasted this from old code so all variables may not apply to your situation
1. action is a parameter needed for this particular application
2. serviceURL is a complete URL, http://...etc etc
3. if you build a GET request it can not have a body. For a GET
you would have to build the parameters into the URL.
4. ch will only equal -1 when the complete response stream has been received
5. reqT is simply an object that can return the desired body - substitute whatever you want.
Bill
|
 |
 |
|
|
subject: Send a query to the server from my Java Class
|
|
|