Solved

URLConnection for a GET and https?

Posted on 2010-09-17
7
471 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 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