prestashop和Web服务(宁静的),HttpURLConnection类,的BufferedReader宁静、Web、prestashop、HttpURLConnection

2023-09-07 01:24:12 作者:夜未央

我要访问通过RESTful Web服务,这是我进入你必须输入一个密钥(由prestashop产生当我们创建一个RESTful Web服务)的URL由prestashop创建了一个网站的资源在用户名的领域。

I want to access the resources of a site created by prestashop via restful web services, which I enter the URL you must enter a key (that is generated by prestashop when we create a restful web service) in the field of username.

所以我想读一个XML字符串:

so I am trying to read a xml string:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop>
<manufacturers>        
     <manufacturer id="1" xlink:href="http://127.0.0.1/test/api/manufacturers/1" />
     <manufacturer id="2" xlink:href="http://127.0.0.1/test/api/manufacturers/2" />
</manufacturers>
</prestashop>

通过HTTP:

over HTTP:

我有以下的code:

public class MainTest 
{
public static String readUrl(HttpURLConnection conn) throws Exception
{
    BufferedReader reader = null;
    try
    {       
        reader = new BufferedReader(new InputStreamReader((conn.getInputStream())));        
        StringBuffer buffer = new StringBuffer();
        int read;
        char[] chars = new char[1024];
        while ((read = reader.read(chars)) != -1)
            buffer.append(chars, 0, read);          
        return buffer.toString();
    } finally
    {
        if (reader != null)
            reader.close();
    }

}



public static void main(String[] args) throws Exception
{
    URL url = new URL("http://127.0.0.1/test/api/manufacturers");
    HttpURLConnection conn = null;
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setReadTimeout(30000);

    conn.setRequestProperty("Accept", "application/XML");
    conn.setRequestProperty("Authentication-Key", "ALHKUNM0J6JJAQC21E4TVWHBM6FAKACF");
    System.out.println("true2");

    String xml="";

            xml = readUrl(conn);
    System.out.println(xml);


}

}

但它给我这个错误

but it give me this error

 Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401  for URL: http://127.0.0.1/test/api/manufacturers
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.test.services.URLReader.main(URLReader.java:28)

我认为这个问题是在这个LIGNE

i think the problem is in this ligne

reader = new BufferedReader(new InputStreamReader((conn.getInputStream())));

请帮助我,如果您有任何解决方案

please help me if you have any solution

关于

推荐答案

您请求属性进行身份验证是错误的! 首先,prestashop REST API使用基本身份验证。

You request property for authentication is wrong! First, Prestashop REST API is using basic authentication.

然后,你将需要根据的base64加密您的凭据进行加密。 所以下载commons- codeC-1.5.jar从的http://公共资源。 apache.org/proper/commons-$c$cc/ 。 这是我做的方式。

Then you will need to encrypt your credentials based on base64 encryption. So download commons-codec-1.5.jar from http://commons.apache.org/proper/commons-codec/. Here is the way I did it.

    import org.apache.commons.codec.binary.Base64
    //.....
    String username  = "Your Prestashop webservice key";
    String password = "";// leave it empty
String authToBytes = username + ":" + password;
    //....
    byte authBytes = Base64.encodeBase64(authToBytes.getBytes())// I keep it generic
    String authBytesString =  new String(authBytes);
    //then your code
    conn.setRequestProperty("Authorization", "Basic " + authBytesString);
//...

它应该现在的工作。

It should work now.

找小prestashop的Java API时的 http://www.onasus.com/2012/10/3712/$p$pstashop-web-services-java-api/

Find a small Prestashop java API at http://www.onasus.com/2012/10/3712/prestashop-web-services-java-api/

我发现很多很多其他的方式来使用的Web服务。其中一人使用类java.net.Authenticator中自动处理HTTP基本身份验证为您服务。欲知详情,http://examples.java$c$cgeeks.com/core-java/net/authenticator/access-password-protected-url-with-authenticator/

I found many many other ways to consume the web services. One of them uses the class java.net.Authenticator which handles the HTTP Basic authentication for you automatically. Find out more at http://examples.javacodegeeks.com/core-java/net/authenticator/access-password-protected-url-with-authenticator/ .