Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Send a query to the server from my Java Class

 
Isaac Ferguson
Ranch Hand
Posts: 835
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Isaac Ferguson
Ranch Hand
Posts: 835
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic