aspose file tools*
The moose likes Java in General and the fly likes Send a query to the server from my Java Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Send a query to the server from my Java Class" Watch "Send a query to the server from my Java Class" New topic
Author

Send a query to the server from my Java Class

Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 358
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: 12789
    
    5
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

Joined: Jun 22, 2012
Posts: 358
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: 12789
    
    5

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Send a query to the server from my Java Class