aspose file tools*
The moose likes Servlets and the fly likes add custom header before submitting form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "add custom header before submitting form" Watch "add custom header before submitting form" New topic
Author

add custom header before submitting form

Deyan Sultov
Ranch Hand

Joined: Jul 01, 2002
Posts: 32
Hello all,

is it possible to add custom headers before submitting a form?

[problem]
I have to implement automatic login to a site which uses Basic Authentication. I need somehow to add the following header:
Authorization Basic base64enc(user : pass)
before submiting to the protected site.

Thanks in advance,
Deyan
[ May 09, 2005: Message edited by: Deyan Sultov ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Isn't this the same question as:
http://www.coderanch.com/t/360419/Servlets/java/Automatic-Basic-Authentication
?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Submitting a form from where? What client?
If you are (for example) creating request from a Java client, you can set request properties with the method in URLConnection.
Bill
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Yes, here is some my code, I can send a complete though:
boolean checkCredentials() {
String credentials = req.getHeader(AUTHORIZATION);
if (credentials!=null) {
int p = credentials.indexOf(' ');
if (p > 0 && credentials.substring(0, p).equalsIgnoreCase("Basic")) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new sun.misc.BASE64Decoder().decodeBuffer(new StringBufferInputStream(credentials.substring(p+1)), baos);
credentials = baos.toString();
p = credentials.indexOf(':');
if (p > 0) {
String password = credentials.substring(p+1);
String user = credentials.substring(0, p);
if ("Administrator".equals(user) &&
dispatcher.getProperty(PROP_PASSWORD, "")
.equals(Crypt.crypt("", password)))
return true;
}
} catch(IOException e) {
dispatcher.log(e.getMessage());
}
} else {
try {
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED , "Not supported authorization type.");
} catch(IOException e) {
dispatcher.log(e.getMessage());
}
return false;
}
}
resp.setHeader(WWW_AUTHENTICATE, "Basic realm=\"Payment Administrator\"");
try {
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Provide your credentials for the authorization.");
resp.flushBuffer();
} catch(IOException e) {
dispatcher.log(e.getMessage());
}
return false;
}


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
Hi,

i never used this sort of authentification, so my answer might slightly miss the point.

But if you send your form from a webbrowser...

You can add key value pairs to the http header with a HTML Meta



J.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Yes, here is some my code, I can send a complete though:

That appears to be server side code - but your question was about adding custom headers to a request on the client side.
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Sorry, you are right.
 
 
subject: add custom header before submitting form