private static Logger _logger = Logger.getLogger("org.owasp.webscarab.util.RequestConverter");
public static Request convertGetToPost(Request get) {
if (!"GET".equals(get.getMethod()))
throw new IllegalArgumentException("Request must be a GET, not a " + get.getMethod());
Request post = new Request();
post.setMethod("POST");
HttpUrl url = get.getURL();
String query = url.getQuery();
if (query != null) {
try {
post.setContent(query.getBytes("ASCII"));
} catch (UnsupportedEncodingException uee) {
_logger.severe("Bizarre! " + uee.getLocalizedMessage());
RuntimeException e = new IllegalArgumentException("Unknown ASCII encoding!");
e.initCause(uee);
throw e;
}
String s = url.toString();
int q = s.indexOf('?');
s = s.substring(0, q);
try {
post.setURL(new HttpUrl(s));
} catch (MalformedURLException mue) {
throw new RuntimeException("Couldn't extract the POST url!", mue);
}
} else {
post.setURL(url);
}
post.setVersion(get.getVersion());
post.setHeaders(get.getHeaders());
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setHeader("Content-Length", Integer.toString(query == null ? 0 : query.length()));
return post;
}