Showing posts with label http. Show all posts

How to forward current Session in HttpURLConnection?

Server Room


HttpURLConnection
HttpURLConnection is helps to use HTTP-specific features. Each HttpURLConnection instance is used to make a single request however it can be shared by other instances at the server. The most common use of HttpURLConnection is to make HTTP connection with Cookies, CustomHeader, Set GET/POST Methods, etc...

I used HttpURLConnection to make HTTP call from Servlet, I also required current session to be forwarded to called Servlet. You may have your different scenario.

Source Code
try {
 /* ID of Session you want to forward */
 String sessionId = "";
 /* Reading response data */
 String inputLine = "";

 URL data = new URL("http://www.your-domain.com/your-page.jsp");
 /* comment below line in case of Proxy */
 HttpURLConnection con = (HttpURLConnection) data.openConnection();
 /* Forward current session in request */
 con.setRequestProperty("Cookie", "JSESSIONID=" + URLEncoder.encode(sessionId, "UTF-8"));
 /* Read webpage coontent */
 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
 /* Read line by line */
 while ((inputLine = in.readLine()) != null) {
  System.out.println(inputLine);
 }
 /* close BufferedReader */
 in.close();
 /* close HttpURLConnection */
 con.disconnect();
} catch (Exception e) {
 e.printStackTrace();
}

How Session forwarded/passed in HttpURLConnection request?
Server access Cookies to get Session ID value. JSESSIONID cookie used by server to identify session. If Session ID is valid then server allows you to access that session. This is how it works.

I tested code with Apache Tomcat and JBoss and its working fine. I don't know about other web server but It should work because all web server uses common standards.

Java Documentation: http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html

java.lang.IllegalStateException: More than the maximum number of request parameters (GET plus POST) for a single request ([512]) were detected.


This is very unusual error thrown by Jboss server. Error it self explain that you are passing more than 512 parameters in request. Which is not good practice but if situation drives you in that way. There is solution available for it.

"To change this limit, set the maxParameterCount attribute on the Connector." this message seems informative but actually its wrong message in case of JBoss 7 or may be in future versions.

Error message you've
java.lang.IllegalStateException: More than the maximum number of request parameters (GET plus POST) for a single request ([512]) were detected. Any parameters beyond this limit have been ignored. To change this limit, set the maxParameterCount attribute on the Connector.
 org.apache.tomcat.util.http.Parameters.addParameter(Parameters.java:199)
 org.apache.tomcat.util.http.Parameters.processParameters(Parameters.java:383)
 org.apache.tomcat.util.http.Parameters.processParameters(Parameters.java:229)
 org.apache.catalina.connector.Request.parseParameters(Request.java:2874)
 org.apache.catalina.connector.Request.getParameter(Request.java:1291)
 org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:363)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
Solution:
We need to use org.apache.tomcat.util.http.* library to solve this error. You don't need to import or download this library. It already available in Jboss. All you need to increase value of Parameter Count.

Jboss 7
Step 1: Open standalone.xml file available in "jboss7\standalone\configuration" folder.
Step 2: Paste below code after </extensions> tag and change value as per your requirement.
<system-properties>
   <property name="org.apache.tomcat.util.http.Parameters.MAX_COUNT" value="2000"/>
</system-properties>
Jboss 6 or lower (I didn't try but it has to work in lower version of Jboss as suggested by informative message)
Step 1: Open standalone.xml file available in "jboss6\standalone\configuration" folder.
Step 2: Find connector tag same as follow and add maxParameterCount attribute.
<connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http" maxParameterCount="1000" />