• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

URLConnection for a GET and https?

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
DJ_AM_Juicebox
Asked:
DJ_AM_Juicebox
2 Solutions
 
Dushyant SharmaCommented:
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
 
CEHJCommented:
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
 
DJ_AM_JuiceboxAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CEHJCommented:
>>Do I need to do anything special besides just replacing the url with "https"?

No
0
 
objectsCommented:
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
 
DJ_AM_JuiceboxAuthor Commented:
I have tried to accept the answers but I keep getting a response saying I'm trying to close the question?
0
 
CEHJCommented:
:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now