Solved

Constructing a POST HTTPS Request (EMPHASIS ON USERNAME & PASSWORD)

Posted on 2011-09-29
15
434 Views
Last Modified: 2012-05-12
How can I chuck in user credentials (username and password) in an HTTPS POST request in Java??!
0
Comment
Question by:snajalm
  • 8
  • 7
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36814142
Normally, that's no different from http:

http://www.exampledepot.com/egs/java.net/Auth.html
0
 

Author Comment

by:snajalm
ID: 36814183
But how do you setRequestProperty to construct your POST method?!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36814336
That's a different question, but

http://www.exampledepot.com/egs/java.net/Post.html
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:snajalm
ID: 36814368
But there is nothing in this link...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36814426
0
 

Author Comment

by:snajalm
ID: 36814556
But this link doesn't say anything about how to add "username" and "password" to the POST arguments before sending the request!  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36814605
Normally you don't do that - authentication is part of the protocol and the responsibility of the Authenticator. If in your case you do, then it would be no different from any other post parameters
0
 

Author Comment

by:snajalm
ID: 36815037
so would it be something like,

comm.setRequestProperty("user", userName);
comm.setRequestProperty("password", password);

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36815098
If the server expects them to arrive as headers, then yes
0
 

Author Comment

by:snajalm
ID: 36898372
Can you please let me know of all the possible ways of constructing a post request?  I just want to send the username and password as clear text through POST request!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36899089
>>I just want to send the username and password as clear text through POST request!

Well i already posted a link to an example of that. Just change

>>String data = "width=50&height=100";

to

String data = "username=foo&password=bar";
0
 

Author Comment

by:snajalm
ID: 36899238
I found a couple of cool links that help with making an HTTP POST request and make life a whole lot easier.

http://www.java2s.com/Code/Java/Apache-Common/HttppostmethodExample.htm

http://www.devx.com/Java/Article/17679/0/page/1
http://myjavatools.com/

Regards
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 36899269
>>
http://www.devx.com/Java/Article/17679/0/page/1
http://myjavatools.com/
>>

The above seem to be the same code. Personally i wouldn't use arbitrary code from the web, favouring instead mature APIs such as

>>http://www.java2s.com/Code/Java/Apache-Common/HttppostmethodExample.htm

Having said that, such an API is probably overkill for such a simple requirement (unless you're going to go on to do more complex things, such as cookie management) and you can get along just fine with the generic JRE code i posted without an extra  API
0
 

Author Closing Comment

by:snajalm
ID: 36928665
Thanks heaps for helping through this question!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36930292
No problem. As it happens, i'm working on a reusable https server at the moment and it makes stuff like key management quite simple (using Ant)
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javap bin 2 41
xampp tool 12 56
eclipse console opening separately 2 29
Java: anonymous class 4 29
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

821 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