gpt4 book ai didi

java - struts2中的http post方法

转载 作者:行者123 更新时间:2023-11-30 07:09:40 24 4
gpt4 key购买 nike

当我尝试通过 Advance REST 客户端(google chrome 插件)执行这个 alfresco webscript [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 状态 401 -

type 状态报告

message

description 此请求需要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:指定至少需要guest身份验证
  3. user:指定至少需要命名用户身份验证
  4. admin:指定至少需要指定的管理员身份验证

注意:如果不指定,默认值为none

注意:可选的 runas 属性可用于强制以特定用户身份执行网络脚本。这只能为存储在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com