Solved

URLConnection for a GET and https?

Posted on 2010-09-17
7
463 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:ddsh79
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now