Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using SSL on HttpURLConnection  RSS feed

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

i'm just wondering how i would be able to send a form that is sent via SSL using the openConnection() function of the URL object ?


thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use the javax.net.ssl.HttpsURLConnection class, not java.net.HttpURLConnection.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which means using https as the URL protocol, not http.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah but isnt there more to it than just typing new URL("https://website.com").openConnection() ??
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit more, unless the server certificate is part of the client's truststore already. But you can turn off certificate validation. See e499 and e502 in http://www.exampledepot.com/egs/javax.net.ssl/pkg.html for more details.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

i found an example online but it doesnt seem to work

here is the code




i was using this studi webpage to test .. it is the page the login form will be validated .. i didnt send any login data in the body but still
it should give me a different response than this

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It probably has nothing to do with this problem, but: Don't println to a Socket
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i changed it to



but it doesnt change anything
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure POST is the right method to use? For accessing a page, GET seems likelier.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the page specified is the target of a form action for a login form so it's post ..

here thats the original tag

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, but if it's part of a form then there should be other parameters, like username and password, no? The code you posted doesn't send those, which might well explain the server error.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i thought that w/o sending parameters there would be a generated error message for the user and no server error ....

how would i write post data ... same as i would with a regular http post request using outputstream ?
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That particular error you’re getting is because that is, in fact, an invalid HTTP 1.1 request. I strongly recommend using a tested client API instead of trying to do HTTP yourself, like URL.openConnection() or the Jakarta Commons HttpClient.

There is a quite a lot to do to implement HTTP 1.1 POST correctly, like including the correct Host, Connection, Date, Content-Type and Content-Length headers, encoding the query parameters, handling chunked encoding in replies that the one you included, etc. The HttpClient PostMethod will do all this for you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!