[英]http post method in struts2
當我嘗試通過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;
}
}
當我調試它,然后我得到了resonseString Apache Tomcat等/ 6.0.29 -錯誤報告
類型狀態報告
信息
說明此請求需要HTTP認證()。
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>
好吧,我的猜測是,您在戶外的Google Chrome瀏覽器的另一個標簽中通過了身份驗證,並且在戶外進行了身份驗證。 401是身份驗證異常,您需要身份驗證到露天,這在上面的代碼中未完成。 例如,請參閱: http : //wiki.alfresco.com/wiki/Web_Scripts#Authenticating
您應該做的第一件事是檢查您的Web腳本描述文件,以找到所需的身份驗證方法。 由於這似乎是您在露天安裝中的自定義網頁腳本,因此很難告訴您在哪里可以找到它。 可以將其稱為類似get-order.info.post.desc.xml的內容(對名為get-BTW的腳本的后處理請求很奇怪)看一下身份驗證元素。
仔細檢查您的描述文件。 並檢查在開發Web腳本時要提供的身份驗證級別。
在webscript desc.xml文件中,身份驗證(可選)是必需的身份驗證級別; 有效值為:
注意 :如果未指定,則默認值為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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.