0
我有用Liferay編寫的web服務。我想要在單獨的機器上運行的Java Client中獲取數據。我可以這樣做嗎? 我都試過,但我得到以下錯誤如何從java客戶端使用Liferay編寫的liferay web服務?
{「消息」:「身份的訪問要求」,「異常」:「java.lang.SecurityException異常」}
我的代碼如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class JavaClient {
// http://localhost:8080/RESTfulExample/json/product/get
public static void main(String[] args)
{
try
{
// URL url = new
// URL("http://localhost:8080/api/jsonws/us-pharmacy-ui-portlet.ph_fax/get-documents-to-fax
// \-u [email protected]:test");
HttpURLConnection conn = (HttpURLConnection) getURL().openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if(conn.getResponseCode() != 200){ throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); }
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null)
{
System.out.println(output);
}
conn.disconnect();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static URL getURL() throws MalformedURLException
{
String url = "http://localhost:8080";
String screenName = "shahid.rana";
String password = "123";
int pos = url.indexOf("://");
String protocol = url.substring(0, pos + 3);
String host = url.substring(pos + 3, url.length());
StringBuilder sb = new StringBuilder();
sb.append(protocol);
sb.append(screenName);
sb.append(":");
sb.append(password);
sb.append("@");
sb.append(host);
sb.append("/api/jsonws/us-pharmacy-ui-portlet.ph_fax/get-documents-to-fax/");
// sb.append(serviceName);
System.out.println("sb.toString()" + sb.toString());
return new URL(sb.toString());
}
}
嗨PARTH Ghiya我得到如下因素誤差..HttpHost不能被解析爲一個類型和BasicHttpContext不能被解析爲一個類型 –
@ SajjadHussain你將需要這許多jar files.commons-codec.jar ,httpclient.jar,httpcore.jar,commons-logging.jar如果你想跟着我的片段 –
我已經有以下罐子commons-codec-1.6.jar commons-codec-1.7.jar httpclient-4.0.jar httpclient-4.1-beta1.jar,httpcore.jar –