これはクラウド上でのみ機能し、GAE に適しています。
jsoupが必要です。
package some.package
import java.util.HashMap;
import java.util.Map;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AppEngineScraperUtil
{
private static final Logger logger = LoggerFactory.getLogger( AppEngineScraperUtil.class );
/**
* @param appId
* in the form of {@code appId}
* @return dashboard in html
*/
public static String fetchDashboard( String appId )
{
return jsoupWay( appId );
}
private static String jsoupWay( String appId )
{
try
{
Connection conn = createGetConn( "https://appengine.google.com" );
Response result = conn.execute();
Document doc = result.parse();
// parse inputs
Elements elements = doc.select( "#dsh, [name=GALX], #service, #continue, #ltmpl" );
Map<String, String> formFields = new HashMap<String, String>();
// build form
for ( Element element: elements )
formFields.put( element.attr( "name" ), element.val() );
formFields.put( "Email", "xxx" );
formFields.put( "Passwd", "xxx" );
String formAction = doc.select( "form" ).first().attr( "action" );
// parse cookies
Map<String, String> cookies = result.cookies();
// build post
conn = createPostConn( formAction );
conn.cookies( cookies );
conn.data( formFields );
conn.header( "Content-Type", "application/x-www-form-urlencoded" );
result = conn.execute();
doc = result.parse();
// get dashboard
conn = createGetConn( "https://appengine.google.com/dashboard?&app_id=" + appId );
conn.cookies( result.cookies() );
result = conn.execute();
// return html
doc = result.parse();
return doc.toString();
}
catch ( Exception e )
{
logger.error( "Error retrieving dashboard.", e );
}
return null;
}
private static Connection createPostConn( String url )
{
Connection conn = Jsoup.connect( url );
conn.method( Method.POST );
return conn;
}
private static Connection createGetConn( String url )
{
Connection conn = Jsoup.connect( url );
conn.method( Method.GET );
return conn;
}
}