Tema: Curl en Jsp
Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/09/2011, 15:38
montidev
 
Fecha de Ingreso: septiembre-2011
Mensajes: 2
Antigüedad: 12 años, 7 meses
Puntos: 0
Exclamación Curl en Jsp

Hola gente.
Vengo de php y no se nada de jsp, pero surgió la necesidad de pasar un código php a jsp que básicamente envia por post una serie de parametros a una url por post y obtiene una respuesta del servidor.
Luego de buscar bastante encontre una solución en la pagina de Oracle pero me pincha por todos lados, cuando lo publico en Tomcat.
El codigo original es el siguelte:
<?php
$affiliateid=78;
$adtype=3;
$request = curl_init();
$params = array('rt=' . $rt,
'f='. $affiliateid,
't='. $adtype,
'z=' . ($sec + $usec),
'u=' . urlencode($_SERVER['HTTP_USER_AGENT']),
'i=' . urlencode($_SERVER['REMOTE_ADDR']),
'p=' . urlencode("$protocol://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']),
'v=' . urlencode('20080605-PHPCURL-hkop0040bcdea227'));
$post = implode('&', $params);
curl_setopt($request, CURLOPT_URL, "http://www.misitio.mobi/archivo.php");
curl_setopt($request, CURLOPT_HEADER, false);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Connection: Close'));
curl_setopt($request, CURLOPT_POSTFIELDS, $post);
curl_exec($request);
curl_close($request);
?>

Yo encontre un link que podria servir:
http://developers.sun.com/mobility/midp/ttips/HTTPPost/

y me quedo asi:
<%
String url = "http://www.misitio.mobi/archivo.php";
HttpConnection conn = (HttpConnection) Connector.open( url ) ;
String affiliateid = "28";
String adtype = "1";

String agent = "Mozilla/4.0";
String rawData = "f=28&t=1&v=20081105-PHPCURL-tuio0040tyui222";
String type = "application/x-www-form-urlencoded";

String encodedData = encode( rawData ); // user-supplied

try {
conn = (HttpConnection) Connector.open( url );
conn.setRequestMethod( HttpConnection.POST );
conn.setRequestProperty( "User-Agent", agent );
conn.setRequestProperty( "Content-Type", type );
conn.setRequestProperty( "Content-Length",
encodedData.length() );

OutputStream os = conn.openOutputStream();
os.write( encodedData.getBytes() );

int rc = conn.getResponseCode();
// process it
}
catch( IOException e ){
// handle the error here
}
%>

Pero me da errores por todos lados del siguiente tipo:
org.apache.jasper.JasperException: No se puede compilar la clase para JSP:
HttpConnection cannot be resolved to a type
Connector cannot be resolved
The method encode(String) is undefined for the type
HttpConnection.POST cannot be resolved to a type
OutputStream cannot be resolved to a type
IOException cannot be resolved to a type

¿Alguien me podra dar una mano? Estoy re perdidoooo!!!
Gracias de antemano

Última edición por montidev; 26/09/2011 a las 15:44