• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Send a query to the server from my Java Class

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic