You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Gerdes, Tom" <TG...@OldRepublicTitle.com> on 2006/03/27 23:57:26 UTC

Using "removeRequestHeader" method.

I am trying to remove request headers from my "PostMethod" object before
sending a post operation to a server.   I use the "removeRequestHeader"
method as shown below.   Although, in my log it shows the request
headers as still being sent when I use the method "getRequestHeaders".
Am I using the "removeRequestHeader" correctly.  Are you the right group
to ask question of.  I am using commons Httpclient 3.0.

 

Java source code:

  System.out.println("This is the URI after get3: " + CurrentURI);

  System.out.println("The URI post3:
https://www.netteller.com/anchorlink/hbMain.cfm");

  PostMethod post3 = new
PostMethod("https://www.netteller.com/anchorlink/hbMain.cfm");

  post3.getParams().setParameter("http.socket.timeout", new
Integer(5000));

  post3.setRequestHeader("Accept", "image/gif, image/x-xbitmap,
image/jpeg, image/pjpeg, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword,
application/x-shockwave-flash, */*");

  post3.removeRequestHeader("User-Agent");

  post3.removeRequestHeader("Host");

  post3.removeRequestHeader("Content-Length");

  post3.removeRequestHeader("Content-Type");

  post3.setRequestHeader("Connection", "Keep-Alive");

  try { theclient.executeMethod(post3); }

  catch (HttpException he)

   { he.printStackTrace(System.out);

     System.out.println(he.getMessage().toString());

     ErrorMessageText = he.getMessage().toString();

     Email.send(EmailServer, ReplyToEmail, EmailForError, "", "",
"Anchor Bank - Positive Pay Error!", "Error Sending User Name and
Password to web site https://www.netteller.com/anchorlink/hbMain.cfm.
Method post3 in BANKARJ failed!  Here is the error Message: " +
ErrorMessageText, "", ntUserName, ntPassword, ReplyToEmail, "");

     BANKARJ.logoff(theclient, EmailServer, EmailForError, ntUserName,
ntPassword, ReplyToEmail);

   }

  catch (IOException ie)

   { ie.printStackTrace(System.out);

     System.out.println(ie.getMessage().toString());

     ErrorMessageText = ie.getMessage().toString();

     Email.send(EmailServer, ReplyToEmail, EmailForError, "", "",
"Anchor Bank - Positive Pay Error!", "Error Sending User Name and
Password to web site https://www.netteller.com/anchorlink/hbMain.cfm.
Method post3 in BANKARJ failed!  Here is the error Message: " +
ErrorMessageText, "", ntUserName, ntPassword, ReplyToEmail, "");

     BANKARJ.logoff(theclient, EmailServer, EmailForError, ntUserName,
ntPassword, ReplyToEmail);

   }

  System.err.println(post3.getStatusLine().toString());

  CurrentURI = post3.getURI().toString();

  System.out.println("Here are the post3 Request Headers:");

  Header[] ReqHeadersp3 = post3.getRequestHeaders();

  for (int i=0; i<ReqHeadersp3.length; i++){
System.out.println(ReqHeadersp3[i]); }

  System.out.println("Here are the post3 Response Headers:");

  Header[] RspHeadersp3 = post3.getResponseHeaders();

  for (int i=0; i<RspHeadersp3.length; i++){
System.out.println(RspHeadersp3[i]); }

  System.err.println("Here are the new cookies after post3:");

  Cookie[] p3aCookies = httpstate.getCookies();

  for (int i=0; i<p3aCookies.length; i++){ System.err.println(i + ". " +
p3aCookies[i]); }

  //System.out.println("Here is the post3 Response Body:");

  //System.out.println(post3.getResponseBodyAsString());

  post3.releaseConnection();

 

Here is a clip out of my log:

 

Here are the post3 Request Headers:   

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-excel, application/vnd.ms-powerpoint, application

 /msword, application/x-shockwave-flash, */*


Connection: Keep-Alive


User-Agent: Jakarta Commons-HttpClient/3.0


Host: www.netteller.com


Cookie: $Version=0; ASP.NET_SessionId=mznsco45afcwmrby55bsif45; $Path=/


Cookie: $Version=0; JHA=060327205627717e524c1c7bae532d2b32323ab33f40;
$Path=/                                                    

Content-Length: 208


Content-Type: application/x-www-form-urlencoded 

 

Any help you can give would be greatly appreciated!



Re: Using "removeRequestHeader" method.

Posted by Roland Weber <RO...@de.ibm.com>.
Hi Tom,

some request headers are handled automatically by HTTP client,
in order to ensure correct HTTP requests or to support features
that should not require application code. These headers will be
inserted at the time you execute the request. You can not remove
them using removeRequestHeader(). Examples are:

Host:
  is required for HTTP/1.1 - change to HTTP/1.0 to remove
User-Agent:
  taken from the parameters, try (un)setting it there
Content-Type:
  taken from the request entity
Content-Length:
  taken from the request entity if present

You should be aware that removing such headers will interfere
with the operation of the HTTP protocol. For example, you are
trying to enforce connection keep-alive. That requires either a
Content-Length header, or HTTP/1.1 with chunked encoding.
If you want to remove Host and Content-Length headers, you
can't have connection keep-alive. Play at your own risk ;-)

cheers,
  Roland





"Gerdes, Tom" <TG...@OldRepublicTitle.com> 
27.03.2006 23:57
Please respond to
"HttpClient Project"


To
<ht...@jakarta.apache.org>
cc

Subject
Using "removeRequestHeader" method.






I am trying to remove request headers from my "PostMethod" object before
sending a post operation to a server.   I use the "removeRequestHeader"
method as shown below.   Although, in my log it shows the request
headers as still being sent when I use the method "getRequestHeaders".
Am I using the "removeRequestHeader" correctly.  Are you the right group
to ask question of.  I am using commons Httpclient 3.0.

 

Java source code:

  System.out.println("This is the URI after get3: " + CurrentURI);

  System.out.println("The URI post3:
https://www.netteller.com/anchorlink/hbMain.cfm");

  PostMethod post3 = new
PostMethod("https://www.netteller.com/anchorlink/hbMain.cfm");

  post3.getParams().setParameter("http.socket.timeout", new
Integer(5000));

  post3.setRequestHeader("Accept", "image/gif, image/x-xbitmap,
image/jpeg, image/pjpeg, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword,
application/x-shockwave-flash, */*");

  post3.removeRequestHeader("User-Agent");

  post3.removeRequestHeader("Host");

  post3.removeRequestHeader("Content-Length");

  post3.removeRequestHeader("Content-Type");

  post3.setRequestHeader("Connection", "Keep-Alive");

  try { theclient.executeMethod(post3); }

  catch (HttpException he)

   { he.printStackTrace(System.out);

     System.out.println(he.getMessage().toString());

     ErrorMessageText = he.getMessage().toString();

     Email.send(EmailServer, ReplyToEmail, EmailForError, "", "",
"Anchor Bank - Positive Pay Error!", "Error Sending User Name and
Password to web site https://www.netteller.com/anchorlink/hbMain.cfm.
Method post3 in BANKARJ failed!  Here is the error Message: " +
ErrorMessageText, "", ntUserName, ntPassword, ReplyToEmail, "");

     BANKARJ.logoff(theclient, EmailServer, EmailForError, ntUserName,
ntPassword, ReplyToEmail);

   }

  catch (IOException ie)

   { ie.printStackTrace(System.out);

     System.out.println(ie.getMessage().toString());

     ErrorMessageText = ie.getMessage().toString();

     Email.send(EmailServer, ReplyToEmail, EmailForError, "", "",
"Anchor Bank - Positive Pay Error!", "Error Sending User Name and
Password to web site https://www.netteller.com/anchorlink/hbMain.cfm.
Method post3 in BANKARJ failed!  Here is the error Message: " +
ErrorMessageText, "", ntUserName, ntPassword, ReplyToEmail, "");

     BANKARJ.logoff(theclient, EmailServer, EmailForError, ntUserName,
ntPassword, ReplyToEmail);

   }

  System.err.println(post3.getStatusLine().toString());

  CurrentURI = post3.getURI().toString();

  System.out.println("Here are the post3 Request Headers:");

  Header[] ReqHeadersp3 = post3.getRequestHeaders();

  for (int i=0; i<ReqHeadersp3.length; i++){
System.out.println(ReqHeadersp3[i]); }

  System.out.println("Here are the post3 Response Headers:");

  Header[] RspHeadersp3 = post3.getResponseHeaders();

  for (int i=0; i<RspHeadersp3.length; i++){
System.out.println(RspHeadersp3[i]); }

  System.err.println("Here are the new cookies after post3:");

  Cookie[] p3aCookies = httpstate.getCookies();

  for (int i=0; i<p3aCookies.length; i++){ System.err.println(i + ". " +
p3aCookies[i]); }

  //System.out.println("Here is the post3 Response Body:");

  //System.out.println(post3.getResponseBodyAsString());

  post3.releaseConnection();

 

Here is a clip out of my log:

 

Here are the post3 Request Headers: 

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-excel, application/vnd.ms-powerpoint, application

 /msword, application/x-shockwave-flash, */*


Connection: Keep-Alive


User-Agent: Jakarta Commons-HttpClient/3.0


Host: www.netteller.com


Cookie: $Version=0; ASP.NET_SessionId=mznsco45afcwmrby55bsif45; $Path=/


Cookie: $Version=0; JHA=060327205627717e524c1c7bae532d2b32323ab33f40;
$Path=/ 

Content-Length: 208


Content-Type: application/x-www-form-urlencoded 

 

Any help you can give would be greatly appreciated!





---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org