Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

add custom header before submitting form

 
Deyan Sultov
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, you are right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic