简体   繁体   English

struts2中的http post方法

[英]http post method in struts2

when i try to execute this alfresco webscript [http://localhost:8383/alfresco/service/get-order-info] through Advance REST client (google chrome add-on) then it works smoothly but when i try to execute by following code then it gives error at this line JSONObject jsonObject = (JSONObject) new JSONParser().parse(responseString); 当我尝试通过Advance REST客户端(google chrome附加组件)执行此露天网页脚本[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;
}
}

and when i debugged it then i got resonseString like Apache Tomcat/6.0.29 - Error report 当我调试它,然后我得到了resonseString Apache Tomcat等/ 6.0.29 -错误报告

HTTP Status 401 - HTTP状态401-

type Status report 类型状态报告

message 信息

description This request requires HTTP authentication (). 说明此请求需要HTTP认证()。

Apache Tomcat/6.0.29 Apache Tomcat / 6.0.29

content of get-order-info.post.desc.xml : 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>

Well my guess is that you are authenticated in alfresco in another tab of google chrome and that alfresco picks that up. 好吧,我的猜测是,您在户外的Google Chrome浏览器的另一个标签中通过了身份验证,并且在户外进行了身份验证。 401 is an authentication exception and you need to authenticate to alfresco which is not done in your code above. 401是身份验证异常,您需要身份验证到露天,这在上面的代码中未完成。 See for example: http://wiki.alfresco.com/wiki/Web_Scripts#Authenticating 例如,请参阅: http : //wiki.alfresco.com/wiki/Web_Scripts#Authenticating

The first thing you should do is to check you webscripts description file to find out which authentication method it demands. 您应该做的第一件事是检查您的Web脚本描述文件,以找到所需的身份验证方法。 Since this seems to be a custom webscript in you alfresco installation its hard to tell you where it is to be found. 由于这似乎是您在露天安装中的自定义网页脚本,因此很难告诉您在哪里可以找到它。 It could be called something like get-order.info.post.desc.xml (strange with a post request to a script named get- BTW) Look at the authentication element. 可以将其称为类似get-order.info.post.desc.xml的内容(对名为get-BTW的脚本的后处理请求很奇怪)看一下身份验证元素。

Double check your description file. 仔细检查您的描述文件。 and check which level of authentication you want to provide while web script development. 并检查在开发Web脚本时要提供的身份验证级别。

In webscript desc.xml file, authentication (optional) is the required level of authentication; 在webscript desc.xml文件中,身份验证(可选)是必需的身份验证级别; valid values are: 有效值为:

  1. none : specifies that no authentication is required at all none :指定完全不需要身份验证
  2. guest : specifies that at least guest authentication is required guest :指定至少需要访客身份验证
  3. user : specifies that at least named user authentication is required user :指定至少需要指定的用户身份验证
  4. admin : specifies that at least a named admin authentication is required admin :指定至少需要一个命名的admin身份验证

Note : if not specified, the default value is none 注意 :如果未指定,则默认值为none

Note : The optional runas attribute can be used to force the execution of a web script as a specific user. 注意 :可选的runas属性可用于强制以特定用户的身份执行Web脚本。 This can only be specified for web scripts that are stored in the Java Class path. 只能为存储在Java类路径中的Web脚本指定此选项。

refer the following link for more details: http://wiki.alfresco.com/wiki/Web_Scripts 请参阅以下链接以获取更多详细信息: http : //wiki.alfresco.com/wiki/Web_Scripts

Or else if you want to keep your web script for only authenticated users, then you need to pass required authentication details for the user who is accessing the web script from struts. 否则,如果您只想为经过身份验证的用户保留Web脚本,则需要为从Struts访问Web脚本的用户传递所需的身份验证详细信息。 But make sure that the user must exists in alfresco. 但是请确保用户必须存在于露天场所。

So, add following code in your fetchComplainInfo method for basic authentication: 因此,在您的fetchComplainInfo方法中添加以下代码以进行基本身份验证:

String basic_auth = new String(Base64.encodeBase64((YOUR_USER_NAME+":"+YOUR_PASSWORD).getBytes()));
httpPost.addHeader("Authorization", "Basic " + basic_auth);

So, your method will be like this: 因此,您的方法将如下所示:

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;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM