Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java: entering a secure website with userid and password  RSS feed

 
Eric Giang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use Java to enter a secure website that requests username and password. How do we even go about posting the userid and password.

I found smthg like this:
String parameters = URLEncoder.encode("inst").concat("=").concat(
URLEncoder.encode(inst));
URL url = new URL("https://jdfg?"+parameters);
HttpsURLConnection urlc = null;
urlc = (HttpsURLConnection) url.openConnection();
urlc.setRequestProperty("Content-Type","Text/xml; charset=\"utf-8\"");
urlc.setRequestMethod("POST");
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
OutputStreamWriter outStream = new OutputStreamWriter(new BufferedOutputStream(urlc.getOutputStream()),"ASCII");
System.out.println("Query String"+parameters);
outStream.write(parameters);
outStream.flush();
outStream.close();
urlc.setRequestProperty("Content-Type", "text/html");
urlc.connect();

//get response
InputStream is = urlc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.println(urlc.getResponseMessage());
//reading the response recieved
String response = System.getProperty("line.separator")+ br.readLine();
System.out.println(response);

If I try to exceute the same piece of code by appending paramters to the URL after the ? ,everything works fine.
URL url = new URL("https://sdfsdg?"+parameters);
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!