java.io.IOException:服务器返回HTTP响应代码:401

时间:2014-07-28 18:11:59

标签: java tomcat servlets https http-status-code-401

我收到以下错误:

Jul 28, 2014 10:57:56 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet TEST_Authenticate threw exception
java.io.IOException: Server returned HTTP response code: 401 for URL: http://myurl.com/authenticate
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
    at TEST_Authenticate.sendHttpRequest(TEST_Authenticate.java:131)
    at TEST_Authenticate.AddNewCpp(TEST_Authenticate.java:103)
    at TEST_Authenticate.doGet(TEST_Authenticate.java:73)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:744)

这是代码片段,其中定义了第131,103和73行。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
        // Allocate a output writer to write the response message into the network socket
        PrintWriter out = response.getWriter();

        // Use ResourceBundle to keep localized string in "LocalStrings_xx.properties"

        // Write the response message, in an HTML page
        try {
            out.println("<!DOCTYPE html");  // HTML 5
            out.println("<html><head>");
            out.println("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");

            out.println(AddNewCpp()); LINE #73

            out.println("<head><title>Test API</title></head>");
            out.println("<body>");
            out.println("<h3>TestAPI</h3>");

            // Tabulate the request information 
            out.println("</body></html>");
        }   
        finally {
            out.close();  // Always close the output writer
        }
    }


 public static Object AddNewCpp() throws IOException {
        String accessKey = "myaccesskey";
        String secretKey = "mysecretkey";
        String uRLCppList = "http://myurl.com/authenticate";
        String method = "POST";
        java.util.Date currentTime = new java.util.Date();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");

        // Give it to me in GMT time.
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        String dateTimeString = sdf.format(currentTime);

        String signature = generateSignature(method, secretKey, dateTimeString);
        String authorization = accessKey + ":" + signature;


        String[] result = sendHttpRequest(uRLCppList, "POST", dateTimeString, authorization);

        return result;
    }

 public static String[] sendHttpRequest(String requestUrl, String method, String dateTimeString, String authorization) throws IOException {
        List<String> response = new ArrayList<String>();
        URL url = new URL(requestUrl);
        URLConnection urlConn = url.openConnection();
        urlConn.setRequestProperty("accept", "application/json");
        urlConn.setRequestProperty("datetime", dateTimeString);
        urlConn.setRequestProperty("authorization", authorization);

        urlConn.setUseCaches(false);

        // the request will return a response
        urlConn.setDoInput(true);

        if ("POST".equals(method)) {
            // set request method to POST
            urlConn.setDoOutput(true);
        } else {
            // set request method to GET
            urlConn.setDoOutput(false);
        }

        // reads response, store line by line in an array of Strings
        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));  LINE # 131

        String line = "";
        while ((line = reader.readLine()) != null) {
            response.add(line);
        }

        reader.close();

        return (String[]) response.toArray(new String[0]);
    }

我正在使用Netbeans 7.4,JDK 8和Java Web Project,即testapi来测试API调用。谁能告诉我我做错了什么?我没有使用正确的API调用吗?

0 个答案:

没有答案