好的,所以我正在尝试使用 Apache HTTPClient 将数据发布到 JSF 页面。如果有人曾经使用过 Geoportal,那就是收割机创建页面。无论如何,我使用 get 请求来获取页面,然后使用包含所有必需参数的 post 请求,包括从 get 请求中解析出来的 JSF 视图状态参数。调用 POST 请求时,页面被返回,但没有执行任何操作,例如保存新的收割机。以下是我的代码,任何帮助将不胜感激。谢谢。
HttpMethod getReq = new GetMethod(pageURI);
getReq.addRequestHeader("Content-Type", "text/html");
getReq.addRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1");
getReq.addRequestHeader("Connection", "keep-alive");
getReq.addRequestHeader("Cookie", "iPlanetDirectoryPro=" + token + "; JSESSIONID=" + sessionid);
try {
// Get the edit harvester page.
int response = client.executeMethod(getReq);
if(response != 200) {
logError(responseObj, response + " error occured while trying to get the Harvester Edit page.");
return responseObj;
}
String responseBody = new String(getReq.getResponseBody(), "UTF-8");
getReq.releaseConnection();
// Extract the View State value.
int position = responseBody.indexOf("javax.faces.ViewState");
if(position == -1) {
logError(responseObj, "Unable to authenticate user. Please make sure your session has not expired.");
return responseObj;
}
responseBody = responseBody.substring(position);
int valPosition = responseBody.indexOf("value");
String valString = responseBody.substring(valPosition + 7, valPosition + 60);
facesValue = valString.substring(0, valString.indexOf("\""));
System.out.println("Faces View: " + facesValue);
}
catch (HttpException e) {
logError(responseObj, "Invalid HTTP retured while trying to get the Harvester Edit page.");
return responseObj;
}
catch (IOException e) {
logError(responseObj, "Unable to get the Harvester Edit page.");
return responseObj;
}
// Build Post Request
PostMethod postReq = new PostMethod(pageURI);
postReq.addRequestHeader("Content-Type", "text/html");
postReq.addRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)");
postReq.addRequestHeader("Accept-Charset", "Chareset+\";q=0.7,*;q=0.7");
postReq.addRequestHeader("Cookie", "iPlanetDirectoryPro=" + token + "; JSESSIONID=" + sessionid);
String userDN = PropertiesService.getProperty("openldap.usersDN");
NameValuePair data[] = {
new NameValuePair("harvestCreate:hvSubmit", "Save"),
new NameValuePair("javax.faces.ViewState", facesValue)
// Note there are many more values here. I just removed them because they should serve no purpose.
};
postReq.setRequestBody(data);
try {
// Get the edit harvester page.
int response = client.executeMethod(postReq);
if(response != 200) {
logError(responseObj, response + " error occured while trying to get the Harvester Edit page.");
return responseObj;
}
String responseBody = new String(postReq.getResponseBody(), "UTF-8");
postReq.releaseConnection();
}
catch (HttpException e) {
logError(responseObj, "Invalid HTTP retured while trying to get the Harvester Edit page.");
return responseObj;
}
catch (IOException e) {
logError(responseObj, "Unable to get the Harvester Edit page.");
return responseObj;
}