Solved

URLConnection for a GET and https?

Posted on 2010-09-17
7
466 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
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
MySQL  on Tomcat 8 70
How to convert from xls to xlsx using java 7 55
pagenation logic how it is working in my code 1 56
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

831 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