Solved

URLConnection for a GET and https?

Posted on 2010-09-17
7
469 Views
Last Modified: 2012-05-10
Hi,

I'm looking at this article about using URLConnection:

    http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html

I need to make a GET request, and read the response. I am forming my url like this:

    String p1 = "a", p2 = "b";
    String url = "https://www.example.com/page?p1=" + p1 + "&p2=" + p2;

can I then just use the first method in that article? I'm not "writing" to the url?:

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

Open in new window


is that ok for my purpose? Also I need to set the url to use https, not sure what if URLConnection will support that?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33701304
URL yahoo = new URL("http://www.yahoo.com/");
URL just need a string paramater and it should be a valid url else it will throw MalformedURLException.

so your string should work
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33701361
Be sure to url-encode the param if necessary (if unsure, doing so does no harm)

http://download.oracle.com/javase/6/docs/api/java/net/URLEncoder.html
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 33701432
But how do I enable https on the above example? Do I need to do anything special besides just replacing the url with "https"? I have used HttpClient in the past and had to add some additional code to get https to work - nothing needs to be done with URLConnection?

Thanks
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 33701443
>>Do I need to do anything special besides just replacing the url with "https"?

No
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 33705470
see the following example

http://helpdesk.objects.com.au/java/how-do-i-send-a-get-request-using-java

it will work with http or https, just need to change the url
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 33707916
I have tried to accept the answers but I keep getting a response saying I'm trying to close the question?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33759718
:)
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 86
ejb on wildfly 5 61
Find out all repos that a user is most active on Github 1 54
How does initial implicit objects  are set up in JSP? 7 37
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question