訪問taotao-portal服務406問題 - HTTP Status 406 (httpclient調用)

 

解決辦法:

1、查看是否Jackson包是否存在。

2、如果後綴是html是不能響應json數據的。需要修改後綴名。

3、POST的編碼亂碼

 

4、正確的用例結果:

D:\devtools\jdk\jdk1.8.0_102\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.4\lib\idea_rt.jar=64305:D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.4\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.4\plugins\junit\lib\junit-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.4\plugins\junit\lib\junit5-rt.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\charsets.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\deploy.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\access-bridge-64.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\cldrdata.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\dnsns.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\jaccess.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\jfxrt.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\localedata.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\nashorn.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\sunec.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\sunjce_provider.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\sunmscapi.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\sunpkcs11.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\ext\zipfs.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\javaws.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\jce.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\jfr.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\jfxswt.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\jsse.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\management-agent.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\plugin.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\resources.jar;D:\devtools\jdk\jdk1.8.0_102\jre\lib\rt.jar;E:\studyNetwork\taotao-mall\taotao-portal\target\test-classes;E:\studyNetwork\taotao-mall\taotao-portal\target\classes;E:\studyNetwork\taotao-mall\taotao-common\target\classes;D:\repository\joda-time\joda-time\2.5\joda-time-2.5.jar;D:\repository\org\apache\commons\commons-lang3\3.3.2\commons-lang3-3.3.2.jar;D:\repository\commons-io\commons-io\1.3.2\commons-io-1.3.2.jar;D:\repository\commons-net\commons-net\3.3\commons-net-3.3.jar;D:\repository\com\fasterxml\jackson\core\jackson-databind\2.4.2\jackson-databind-2.4.2.jar;D:\repository\com\fasterxml\jackson\core\jackson-annotations\2.4.2\jackson-annotations-2.4.2.jar;D:\repository\com\fasterxml\jackson\core\jackson-core\2.4.2\jackson-core-2.4.2.jar;D:\repository\org\apache\httpcomponents\httpclient\4.3.5\httpclient-4.3.5.jar;D:\repository\org\apache\httpcomponents\httpcore\4.3.2\httpcore-4.3.2.jar;D:\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;D:\repository\commons-codec\commons-codec\1.6\commons-codec-1.6.jar;D:\repository\org\slf4j\slf4j-log4j12\1.6.4\slf4j-log4j12-1.6.4.jar;D:\repository\org\slf4j\slf4j-api\1.6.4\slf4j-api-1.6.4.jar;D:\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;E:\studyNetwork\taotao-mall\taotao-manager\taotao-manager-pojo\target\classes;D:\repository\junit\junit\4.12\junit-4.12.jar;D:\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\repository\org\springframework\spring-context\4.1.3.RELEASE\spring-context-4.1.3.RELEASE.jar;D:\repository\org\springframework\spring-aop\4.1.3.RELEASE\spring-aop-4.1.3.RELEASE.jar;D:\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\repository\org\springframework\spring-core\4.1.3.RELEASE\spring-core-4.1.3.RELEASE.jar;D:\repository\org\springframework\spring-expression\4.1.3.RELEASE\spring-expression-4.1.3.RELEASE.jar;D:\repository\org\springframework\spring-beans\4.1.3.RELEASE\spring-beans-4.1.3.RELEASE.jar;D:\repository\org\springframework\spring-webmvc\4.1.3.RELEASE\spring-webmvc-4.1.3.RELEASE.jar;D:\repository\org\springframework\spring-web\4.1.3.RELEASE\spring-web-4.1.3.RELEASE.jar;D:\repository\org\springframework\spring-jdbc\4.1.3.RELEASE\spring-jdbc-4.1.3.RELEASE.jar;D:\repository\org\springframework\spring-tx\4.1.3.RELEASE\spring-tx-4.1.3.RELEASE.jar;D:\repository\org\springframework\spring-aspects\4.1.3.RELEASE\spring-aspects-4.1.3.RELEASE.jar;D:\repository\org\aspectj\aspectjweaver\1.8.4\aspectjweaver-1.8.4.jar;D:\repository\jstl\jstl\1.2\jstl-1.2.jar;D:\repository\javax\servlet\servlet-api\2.5\servlet-api-2.5.jar;D:\repository\javax\servlet\jsp-api\2.0\jsp-api-2.0.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.taotao.httpclient.HttpClientTest,doPost
DEBUG [main] - CookieSpec selected: best-match
DEBUG [main] - Auth cache not set in the context
DEBUG [main] - Connection request: [route: {}->http://localhost:8082][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
DEBUG [main] - Connection leased: [id: 0][route: {}->http://localhost:8082][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
DEBUG [main] - Opening connection {}->http://localhost:8082
DEBUG [main] - Connecting to localhost/127.0.0.1:8082
DEBUG [main] - Connection established 127.0.0.1:64311<->127.0.0.1:8082
DEBUG [main] - Executing request POST /httpclient/post.action HTTP/1.1
DEBUG [main] - Target auth state: UNCHALLENGED
DEBUG [main] - Proxy auth state: UNCHALLENGED
DEBUG [main] - http-outgoing-0 >> POST /httpclient/post.action HTTP/1.1
DEBUG [main] - http-outgoing-0 >> Content-Length: 0
DEBUG [main] - http-outgoing-0 >> Host: localhost:8082
DEBUG [main] - http-outgoing-0 >> Connection: Keep-Alive
DEBUG [main] - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.5 (java 1.5)
DEBUG [main] - http-outgoing-0 >> Accept-Encoding: gzip,deflate
DEBUG [main] - http-outgoing-0 >> "POST /httpclient/post.action HTTP/1.1[\r][\n]"
DEBUG [main] - http-outgoing-0 >> "Content-Length: 0[\r][\n]"
DEBUG [main] - http-outgoing-0 >> "Host: localhost:8082[\r][\n]"
DEBUG [main] - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
DEBUG [main] - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.3.5 (java 1.5)[\r][\n]"
DEBUG [main] - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
DEBUG [main] - http-outgoing-0 >> "[\r][\n]"
DEBUG [main] - http-outgoing-0 << "HTTP/1.1 200 OK[\r][\n]"
DEBUG [main] - http-outgoing-0 << "Server: Apache-Coyote/1.1[\r][\n]"
DEBUG [main] - http-outgoing-0 << "Content-Type: application/json;charset=UTF-8[\r][\n]"
DEBUG [main] - http-outgoing-0 << "Transfer-Encoding: chunked[\r][\n]"
DEBUG [main] - http-outgoing-0 << "Date: Fri, 14 Jun 2019 10:00:58 GMT[\r][\n]"
DEBUG [main] - http-outgoing-0 << "[\r][\n]"
DEBUG [main] - http-outgoing-0 << "25[\r][\n]"
DEBUG [main] - http-outgoing-0 << "{"status":200,"msg":"OK","data":null}[\r][\n]"
DEBUG [main] - http-outgoing-0 << HTTP/1.1 200 OK
DEBUG [main] - http-outgoing-0 << Server: Apache-Coyote/1.1
DEBUG [main] - http-outgoing-0 << Content-Type: application/json;charset=UTF-8
DEBUG [main] - http-outgoing-0 << Transfer-Encoding: chunked
DEBUG [main] - http-outgoing-0 << Date: Fri, 14 Jun 2019 10:00:58 GMT
DEBUG [main] - Connection can be kept alive indefinitely
DEBUG [main] - http-outgoing-0 << "0[\r][\n]"
DEBUG [main] - http-outgoing-0 << "[\r][\n]"
DEBUG [main] - Connection [id: 0][route: {}->http://localhost:8082] can be kept alive indefinitely
DEBUG [main] - Connection released: [id: 0][route: {}->http://localhost:8082][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
{"status":200,"msg":"OK","data":null}
DEBUG [main] - Connection manager is shutting down
DEBUG [main] - http-outgoing-0: Close connection
DEBUG [main] - http-outgoing-0: Close connection
DEBUG [main] - Connection manager shut down

Process finished with exit code 0

 

====================================

 

結果錯誤如下:

<html>
    <head>
        <title>Apache Tomcat/7.0.47 - Error report</title>
        <style>
            <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
        </style>
    </head>
    <body>
        <h1>HTTP Status 406 - </h1>
        <HR size="1" noshade="noshade" />
        <p><b>type</b> Status report</p>
        <p><b>message</b> <u></u></p>
        <p><b>description</b> <u>The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.</u></p>
        <HR size="1" noshade="noshade" />
        <h3>Apache Tomcat/7.0.47</h3>
    </body>
</html>

 

 

1、單元測試用例 

    @Test
    public void doPost() throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //創建一個post對象
        HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");
        //執行post請求
        CloseableHttpResponse response = httpClient.execute(post);
        String string = EntityUtils.toString(response.getEntity());
        System.out.println(string);
        response.close();
        httpClient.close();

    }

 

2、測試接口

    @RequestMapping(value = "/httpclient/post", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult testPost(String username, String password) {
        String result = "username:" + username + "\tpassword:" + password;
        System.out.println(result);
        return TaotaoResult.ok();
    }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章