java - struts2中的http post方法

标签 java struts2 http-post alfresco

当我尝试通过 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.29

get-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(可选)是所需的身份验证级别;有效值为:

  1. none:指定根本不需要身份验证
  2. guest:指定至少需要访客身份验证
  3. 用户:指定至少需要指定用户身份验证
  4. 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/

相关文章:

ios - POST 请求在 Ionic2 应用程序中的 IOS 上不起作用

java - 如何在java GUI中快速更新图像

java - Eclipse RCP 在编辑器中更改后更新 View

java - 如何以编程方式在 Java 中获取二进制数的 2 的补码

java - 使用 java 检查 Apache Velocity(.vm 文件)中的文本

java - Firebase 发送 HttpPost 消息时出现错误 400

java - 如何使用 Java/Android 让按钮打开另一个应用程序?

jsp - 赋予空值的参数标签

java - Struts2 Tiles - 如何实现?

c# - HttpWebRequest - 基础连接已关闭 : An unexpected error occurred on a send