当我尝试通过 Advance REST 客户端(google chrome 插件)执行此 alfresco 网页脚本 [http://localhost:8383/alfresco/service/get-order-info] 时,它可以顺利运行,但是当我尝试通过以下代码执行,然后在这一行给出错误 JSONObject jsonObject = (JSONObject) new JSONParser().parse(responseString);
public class ComplainMasterDaoImpl implements ComplainMasterDao
{
@Override
public ComplainMaster fetchComplainInfo(String orderId, String user) throws Exception
{
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:8383/alfresco/service/get-order-info");
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair("orderId", orderId));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
httpPost.setEntity(formEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String responseString = IOUtils.toString(httpEntity.getContent(), "UTF-8");
JSONObject jsonObject = (JSONObject) new JSONParser().parse(responseString);
JSONObject resultJson = (JSONObject) jsonObject.get("result");
System.out.println(resultJson.toString());
return null;
}
}
当我调试它时,我得到了 resonseString 像 Apache Tomcat/6.0.29 - 错误报告
HTTP Status 401 -
类型状态报告
消息
描述此请求需要HTTP身份验证()。
< h3>Apache Tomcat/6.0.29get-order-info.post.desc.xml的内容:
<webscript>
<shortname>Get Order Information</shortname>
<description>Used to create complain</description>
<url>/get-order-info</url>
<format default="json"> </format>
<authentication>user</authentication>
</webscript>
最佳答案
仔细检查您的描述文件。并检查您希望在 Web 脚本开发时提供哪种级别的身份验证。
在webscript desc.xml文件中,authentication(可选)是所需的身份验证级别;有效值为:
- none:指定根本不需要身份验证
- guest:指定至少需要访客身份验证
- 用户:指定至少需要指定用户身份验证
- admin:指定至少需要指定的管理员身份验证
注意:如果不指定,默认值为none
注意:可选的 runas 属性可用于强制以特定用户身份执行 Web 脚本。只能为存储在 Java 类路径中的 Web 脚本指定此值。
请参阅以下链接了解更多详情: http://wiki.alfresco.com/wiki/Web_Scripts
或者,如果您只想为经过身份验证的用户保留 Web 脚本,那么您需要为从 struts 访问 Web 脚本的用户传递所需的身份验证详细信息。但请确保用户必须存在于露天。
因此,在您的 fetchComplainInfo 方法中添加以下代码以进行基本身份验证:
String basic_auth = new String(Base64.encodeBase64((YOUR_USER_NAME+":"+YOUR_PASSWORD).getBytes()));
httpPost.addHeader("Authorization", "Basic " + basic_auth);
所以,你的方法将是这样的:
public class ComplainMasterDaoImpl implements ComplainMasterDao
{
@Override
public ComplainMaster fetchComplainInfo(String orderId, String user) throws Exception
{
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:8383/alfresco/service/get-order- info");
String basic_auth = new String(Base64.encodeBase64((YOUR_USER_NAME+":"+YOUR_PASSWORD).getBytes()));
httpPost.addHeader("Authorization", "Basic " + basic_auth);
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair("orderId", orderId));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
httpPost.setEntity(formEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String responseString = IOUtils.toString(httpEntity.getContent(), "UTF-8");
JSONObject jsonObject = (JSONObject) new JSONParser().parse(responseString);
JSONObject resultJson = (JSONObject) jsonObject.get("result");
System.out.println(resultJson.toString());
return null;
}
}
关于java - struts2中的http post方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22819908/