Wednesday, 28 August 2013

java.lang.ClassCastException: sun.net.www.protocol.http.HttpURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection

java.lang.ClassCastException: sun.net.www.protocol.http.HttpURLConnection
cannot be cast to javax.net.ssl.HttpsURLConnection

I am writing a code in java(using JSOUP) where i try to log in to flipkart
website. Below is the HTML code where i am parsing specific divs in order
to input required parameters for login and post to the url (mentioned in
my code).
<div class="fk-signin unit ful"> //parent div
<div class="hd">Login</div>
<div class="bd">
<div id="login_tiny_help_message1" class="fk-err-all">Email is wrong.</div>
<div class="line rw">
<div class="unit size1of4 lf">
<label for="login_email_id1">Email Address</label>
</div>
<div class="unit size3of4 lastUnit rt">
<input id="login_email_id1" class="fk-input" type="text" name="email">
//input tag for email
</div>
</div>
<div class="line rw">
<div class="unit size1of4 lf">
<div class="unit size3of4 lastUnit rt">
<input id="login_password1" class="fk-input" type="password
onkeypress="do_login_ful_keyevent(event,'')"
name="password"> //input tag for password
</div>
</div>
<div class="line rw">
<div class="unit size1of4 lf"> </div>
<div class="unit size3of4 lastUnit rt">
<button class="btn btn-blue" onclick="do_login_ful('')">Login</button>
And my java code to add input parameters is:
Element loginform = doc.select("div.bd").first();
Elements inputElements= loginform.getElementsByTag("input");
List<String> paramList = new ArrayList<String>();
for (Element inputElement : inputElements) {
String key = inputElement.attr("name");
String value = inputElement.attr("value");
if (key.equals("email"))
value = username;
else if (key.equals("password"))
value = password;
paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
}
Now after executing my code , i'm getting below execeotion which says:
Sending 'GET' request to URL : https://www.flipkart.com/account/login
Response Code : 200
Extracting form's data...
Sending 'POST' request to URL : https://www.flipkart.com/account/login
Post parameters :
email=krishan11112003%40gmail.com&password=kr11is88han&email=krishan11112003%40gmail.com
Response Code : 200
Exception in thread "main" java.lang.ClassCastException:
sun.net.www.protocol.http.HttpURLConnection cannot be cast to
javax.net.ssl.HttpsURLConnection
at HttpUrlConnectionExample.GetPageContent(HttpUrlConnectionExample.java:104)
at HttpUrlConnectionExample.main(HttpUrlConnectionExample.java:49)
Below is the line on which i am getting exception:
==> conn = (HttpsURLConnection) obj.openConnection();
String result = http.GetPageContent("redirected url");
System.out.println(result);
Can anyone tell me where exactly i'm doing wrong , is it the way of
parsing or casting of class.

No comments:

Post a Comment