【问题标题】:Prestashop and web service(Restful), HttpURLConnection, BufferedReaderPrestashop 和 Web 服务(Restful)、HttpURLConnection、BufferedReader
【发布时间】:2014-03-14 12:18:10
【问题描述】:

我想通过restful web services访问prestashop创建的站点的资源,我输入的URL你必须在用户名字段中输入一个密钥(由prestashop在我们创建一个restful web service时生成)。

所以我正在尝试读取一个 xml 字符串:

<?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:

我有以下代码:

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


}

}

但它给了我这个错误

 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)

我认为问题出在这一行

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

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

问候

【问题讨论】:

    标签: android rest prestashop bufferedreader httpurlconnection


    【解决方案1】:

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

    然后您将需要基于 base64 加密来加密您的凭据。 所以从http://commons.apache.org/proper/commons-codec/下载commons-codec-1.5.jar。 这是我的做法。

        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);
    //...
    

    它现在应该可以工作了。

    http://www.onasus.com/2012/10/3712/prestashop-web-services-java-api/找到一个小型 Prestashop java API

    我发现了许多其他使用 Web 服务的方法。其中之一使用 java.net.Authenticator 类,它会自动为您处理 HTTP Basic 身份验证。在http://examples.javacodegeeks.com/core-java/net/authenticator/access-password-protected-url-with-authenticator/ 了解更多信息。

    【讨论】:

    • 密码字段留空非常重要,如果设置不正确,将无法正常工作。
    【解决方案2】:

    来自HTTP Status Codes

    401 未经授权 类似于 403 Forbidden,但专门用于需要身份验证但已失败或尚未提供身份验证时使用。 [2]响应必须包含一个 WWW-Authenticate 标头字段,该字段包含适用于所请求资源的质询。请参阅基本访问身份验证和摘要访问身份验证。

    尝试找出您的服务需要什么样的身份验证。在使用HttpURLConnection 编写程序之前,通常使用curl 会更容易。

    curl -v  \
         -G  \
         -H 'Accept: application/xml'               \
         -H 'WWW-Authenticate: Basic 938akjsdfh=='  \
         http://127.0.0.1/test/api/manufacturers
    

    另一件事:在访问getInputStream() 之前,您应该始终检查响应代码。如果遇到类似 4xx 的情况,getInputStream() 会抛出异常。在这种情况下,如果您从getErrorStream() 阅读,也许您可​​以获取一些错误信息。

    【讨论】:

      猜你喜欢
      • 2021-01-14
      • 1970-01-01
      • 2012-09-10
      • 2011-03-19
      • 1970-01-01
      • 2013-05-25
      • 2012-09-12
      • 2012-09-10
      • 2017-12-29
      相关资源
      最近更新 更多