Simuler un POST dans une JSP
Bonjour,
j'aimerais simuler l'envoi d'un formulaire avec la methode POST.
Actuellement j'ai une page JSP qui possède un formulaire qui emet la requette suivante :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| http://casatosorigin.dyndns.org:8081/opiam/logon.do
POST /opiam/logon.do HTTP/1.1
Host: casatosorigin.dyndns.org:8081
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://casatosorigin.dyndns.org:8081/opiam/?ticket=ST-22-knGl9t4upFmI4KdUKD2XQ0IUmmKE7vgDmxU-20
Cookie: JSESSIONID=83633B1777912C48804DE0E072882B16; additionalInfoFromCAS=sprain
Content-Type: application/x-www-form-urlencoded
Content-Length: 38
login=scarter&password=sprain&x=35&y=5
HTTP/1.x 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 918
Date: Tue, 12 Jun 2007 14:52:13 GMT |
j'aimerais pourvoir générer la meme requette en me connectant sur une autre JSP.
en gros, j'aimerais realiser une JSP qui créé cette requette, mais je n'y parviens pas. J'ai testé ca :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" %>
<%
String cookieName = "additionalInfoFromCAS";
Cookie cookies [] = request.getCookies ();
Cookie myCookie = null;
String userLogin = null;
String userPassword = null;
if (cookies != null)
{
for (int i = 0; i < cookies.length; i++)
{
if (cookies [i].getName().equals (cookieName))
{
myCookie = cookies[i];
break;
}
}
}
//si le cookie contenant les informations issues de CAS existe,
//on récupère le login et le password.
if (myCookie != null)
{
userLogin = request.getRemoteUser();
userPassword = myCookie.getValue();
}
//envoie de la requette POST pour valider le formulaire d'OPIAM
try {
// Construct data
//String data = java.net.URLEncoder.encode("user", "UTF-8") + "=" + java.net.URLEncoder.encode(userLogin, "UTF-8");
//data += "&" + java.net.URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(userPassword, "UTF-8");
String data = "login=scarter&password=sprain";
// Send data
java.net.URL url = new java.net.URL("http://applisatosorigin.dyndns.org:8080/opiam2/logon.do");
java.net.URLConnection conn = url.openConnection();
conn.setDoOutput(true);
java.io.OutputStreamWriter wr = new java.io.OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
java.io.BufferedReader rd = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
String line;
String pagecontent = "";
while ((line = rd.readLine()) != null) {
// Process line...
pagecontent += line;
}
wr.close();
rd.close();
} catch (Exception e) {
}
%> |
sans succès.
merci pour vos remarques
Aswat
POST avec httpclient dans une JSP
en cherchant d'autres solutions potentielles à mon problème je suis tombé sur le projet HttpClient d'Apache. j'ai donc éssayé d'intégrer ca dans ma JSP, ce qui donne ca :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" %>
<%@ page import="java.io.IOException;"%>
<%@ page import="org.apache.commons.httpclient.HttpClient;"%>
<%@ page import="org.apache.commons.httpclient.HttpException;"%>
<%@ page import="org.apache.commons.httpclient.methods.PostMethod;"%>
<%
String cookieName = "additionalInfoFromCAS";
Cookie cookies [] = request.getCookies ();
Cookie myCookie = null;
String userLogin = null;
String userPassword = null;
if (cookies != null)
{
for (int i = 0; i < cookies.length; i++)
{
if (cookies [i].getName().equals (cookieName))
{
myCookie = cookies[i];
break;
}
}
}
//si le cookie contenant les informations issues de CAS existe,
//on récupère le login et le password.
if (myCookie != null)
{
userLogin = request.getRemoteUser();
userPassword = myCookie.getValue();
}
//envoie de la requette POST pour valider le formulaire d'OPIAM
HttpClient client = new HttpClient();
// Adresse du formulaire à soumettre
String postUrl = "http://applisatosorigin.dyndns.org:8080/opiam2/logon.do";
PostMethod method = new PostMethod(postUrl);
// Champs du formulaire à renseigner
method.addParameter("user", userLogin);
method.addParameter("password", userPassword);
try
{
client.executeMethod(method);
String body = method.getResponseBodyAsString();
// Il est aussi possible de récupérer le résultat sous
// forme de flux InputStream avec getResponseBodyAsStream()
System.out.print(body);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// Libération des ressources
method.releaseConnection();
}
%> |
cependant la JSP ne compile pas, j'ai bien inclus commons-httpclient-3.0.1.jar
et commons-logging.jar
mais j'obtient ca :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| 11:14,261 ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/opiam2].[jsp] - "Servlet.service()" pour la servlet jsp a généré une exception
org.apache.jasper.JasperException: Impossible de compiler la classe pour la JSP:
Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:299)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at edu.yale.its.tp.cas.client.filter.CASFilter.doFilter(CASFilter.java:401)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
at java.lang.Thread.run(Thread.java:595) |