HMdeveloper
HMdeveloper

Reputation: 2874

how to connect to url from java

I have a link of a servlet as follow :

http://localhost:8080/UI/FacebookAuth?code=1

and I wrote a little program to connect this link, if you manually type this link in browser it types something in a console but as soon as I run my code nothing happens, it seems that the link is not executed

System.out.println("Starting...");
URI url = new URI("http://localhost:8080/UI/FacebookAuth?code=1");

HttpGet hg = new HttpGet();
hg.setURI(url);
HttpClient httpclient = new DefaultHttpClient();  
HttpResponse response = httpclient.execute(hg); 


 System.out.println("Finished...");

Can anyone tell me what the problem?

Upvotes: 0

Views: 88

Answers (1)

bstempi
bstempi

Reputation: 2043

Your code snippet does nothing with the response. All you do is print out, "Finished..." Because you threw away the response, you have no way of knowing what happened. Assuming that you're using the Apache HTTP client, you should add something like this:

System.out.println("Status code: " + response.getStatusLine().getStatusCode());

See http://hc.apache.org/httpcomponents-core-4.2.x/httpcore/apidocs/org/apache/http/HttpResponse.html for the methods you can execute on the response.

Upvotes: 2

Related Questions