V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
86322989
V2EX  ›  问与答

用 httpclient 请求 localhost 的一个 url,报 405

  •  
  •   86322989 · 2017-11-08 03:52:09 +08:00 · 3025 次点击
    这是一个创建于 2597 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本地起了一个服务 然后用 httpclient 去 get 结果是 405(不允许的访问)

    查了两天要崩溃了. 有知道的吗

    ===

    ps firebug 看那个 url,都是 get 请求,没有其他比如 post. url 是这样:String s = httpPure("http://localhost:8090/robots.txt");

    7 条回复    2017-11-08 17:11:22 +08:00
    mysterin
        1
    mysterin  
       2017-11-08 11:11:59 +08:00
    请求可能被拦截了
    86322989
        2
    86322989  
    OP
       2017-11-08 12:36:03 +08:00 via iPhone
    @mysterin 被谁拦截
    86322989
        3
    86322989  
    OP
       2017-11-08 15:29:38 +08:00 via iPhone
    用浏览器都能直接访问的
    86322989
        4
    86322989  
    OP
       2017-11-08 16:30:36 +08:00
    具体信息:

    ```
    /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:60042,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/tools.jar:/Users/wangany/Documents/git/shopizer/sm-shop/target/classes:/Users/wangany/Documents/git/shopizer/sm-core/target/classes:/Users/wangany/Documents/git/shopizer/sm-core-modules/target/classes:/Users/wangany/.m2/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar:/Users/wangany/.m2/repository/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar:/Users/wangany/.m2/repository/commons-configuration/commons-configuration/1.6/commons-configuration-1.6.jar:/Users/wangany/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar:/Users/wangany/.m2/repository/commons-digester/commons-digester/2.1/commons-digester-2.1.jar:/Users/wangany/.m2/repository/commons-beanutils/commons-beanutils-core/1.8.0/commons-beanutils-core-1.8.0.jar:/Users/wangany/.m2/repository/org/drools/drools-spring/5.6.0.Final/drools-spring-5.6.0.Final.jar:/Users/wangany/.m2/repository/org/drools/knowledge-api/5.6.0.Final/knowledge-api-5.6.0.Final.jar:/Users/wangany/.m2/repository/org/drools/drools-core/5.6.0.Final/drools-core-5.6.0.Final.jar:/Users/wangany/.m2/repository/org/mvel/mvel2/2.1.8.Final/mvel2-2.1.8.Final.jar:/Users/wangany/.m2/repository/org/drools/knowledge-internal-api/5.6.0.Final/knowledge-internal-api-5.6.0.Final.jar:/Users/wangany/.m2/repository/org/drools/drools-compiler/5.6.0.Final/drools-compiler-5.6.0.Final.jar:/Users/wangany/.m2/repository/org/antlr/antlr-runtime/3.3/antlr-runtime-3.3.jar:/Users/wangany/.m2/repository/org/antlr/antlr/3.3/antlr-3.3.jar:/Users/wangany/.m2/repository/org/antlr/stringtemplate/3.2.1/stringtemplate-3.2.1.jar:/Users/wangany/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar:/Users/wangany/.m2/repository/org/drools/drools-decisiontables/5.6.0.Final/drools-decisiontables-5.6.0.Final.jar:/Users/wangany/.m2/repository/org/drools/drools-templates/5.6.0.Final/drools-templates-5.6.0.Final.jar:/Users/wangany/.m2/repository/org/apache/poi/poi-ooxml/3.9/poi-ooxml-3.9.jar:/Users/wangany/.m2/repository/org/apache/poi/poi/3.9/poi-3.9.jar:/Users/wangany/.m2/repository/org/apache/poi/poi-ooxml-schemas/3.9/poi-ooxml-schemas-3.9.jar:/Users/wangany/.m2/repository/org/apache/xmlbeans/xmlbeans/2.3.0/xmlbeans-2.3.0.jar:/Users/wangany/.m2/repository/stax/stax-api/1.0.1/stax-api-1.0.1.jar:/Users/wangany/.m2/repository/com/thoughtworks/xstream/xstream/1.4.1/xstream-1.4.1.jar:/Users/wangany/.m2/repository/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar:/Users/wangany/.m2/repository/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar:/Users/wangany/.m2/repository/org/infinispan/infinispan-core/6.0.2.Final/infinispan-core-6.0.2.Final.jar:/Users/wangany/.m2/repository/org/infinispan/infinispan-commons/6.0.2.Final/infinispan-commons-6.0.2.Final.jar:/Users/wangany/.m2/repository/org/jgroups/jgroups/3.4.1.Final/jgroups-3.4.1.Final.jar:/Users/wangany/.m2/repository/org/jboss/spec/javax/transaction/jboss-transaction-api_1.1_spec/1.0.1.Final/jboss-transaction-api_1.1_spec-1.0.1.Final.jar:/Users/wangany/.m2/repository/org/jboss/marshalling/jboss-marshalling-river/1.4.4.Final/jboss-marshalling-river-1.4.4.Final.jar:/Users/wangany/.m2/repository/org/jboss/marshalling/jboss-marshalling/1.4.4.Final/jboss-marshalling-1.4.4.Final.jar:/Users/wangany/.m2/repository/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar:/Users/wangany/.m2/repository/org/infinispan/infinispan-cachestore-jdbc/6.0.2.Final/infinispan-cachestore-jdbc-6.0.2.Final.jar:/Users/wangany/.m2/repository/org/infinispan/infinispan-tree/6.0.2.Final/infinispan-tree-6.0.2.Final.jar:/Users/wangany/.m2/repository/org/springframework/boot/spring-boot-starter-test/1.3.5.RELEASE/spring-boot-starter-test-1.3.5.RELEASE.jar:/Users/wangany/.m2/repository/org/mockito/mockito-core/1.10.19/mockito-core-1.10.19.jar:/Users/wangany/.m2/repository/org/objenesis/objenesis/2.1/objenesis-2.1.jar:/Users/wangany/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/wangany/.m2/repository/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3.jar:/Users/wangany/.m2/repository/org/springframework/spring-test/4.2.6.RELEASE/spring-test-4.2.6.RELEASE.jar:/Users/wangany/.m2/repository/org/apache/commons/commons-collections4/4.0/commons-collections4-4.0.jar:/Users/wangany/.m2/repository/commons-validator/commons-validator/1.5.1/commons-validator-1.5.1.jar:/Users/wangany/.m2/repository/commons-beanutils/commons-beanutils/1.9.2/commons-beanutils-1.9.2.jar:/Users/wangany/.m2/repository/org/freemarker/freemarker/2.3.23/freemarker-2.3.23.jar:/Users/wangany/.m2/repository/org/springframework/spring-context-support/4.2.6.RELEASE/spring-context-support-4.2.6.RELEASE.jar:/Users/wangany/.m2/repository/org/apache/httpcomponents/httpclient/4.5.2/httpclient-4.5.2.jar:/Users/wangany/.m2/repository/org/apache/httpcomponents/httpcore/4.4.4/httpcore-4.4.4.jar:/Users/wangany/.m2/repository/commons-codec/commons-codec/1.9/commons-codec-1.9.jar:/Users/wangany/.m2/repository/com/paypal/sdk/merchantsdk/2.6.109/merchantsdk-2.6.109.jar:/Users/wangany/.m2/repository/com/paypal/sdk/paypal-core/1.5.1/paypal-core-1.5.1.jar:/Users/wangany/.m2/repository/com/stripe/stripe-java/1.33.0/stripe-java-1.33.0.jar:/Users/wangany/.m2/repository/com/itextpdf/itextpdf/5.5.6/itextpdf-5.5.6.jar:/Users/wangany/.m2/repository/org/jopendocument/jOpenDocument/1.3b1/jOpenDocument-1.3b1.jar:/Users/wangany/.m2/repository/com/maxmind/geoip2/geoip2/2.7.0/geoip2-2.7.0.jar:/Users/wangany/.m2/repository/com/maxmind/db/maxmind-db/1.2.1/maxmind-db-1.2.1.jar:/Users/wangany/.m2/repository/com/google/http-client/google-http-client/1.21.0/google-http-client-1.21.0.jar:/Users/wangany/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/Users/wangany/Documents/git/shopizer/sm-core-model/target/classes:/Users/wangany/.m2/repository/org/springframework/boot/spring-boot-starter-data-jpa/1.3.5.RELEASE/spring-boot-starter-data-jpa-1.3.5.RELEASE.jar:/Users/wangany/.m2/repository/org/springframework/boot/spring-boot-starter-aop/1.3.5.RELEASE/spring-boot-starter-aop-1.3.5.RELEASE.jar:/Users/wangany/.m2/repository/org/aspectj/aspectjweaver/1.8.9/aspectjweaver-1.8.9.jar:/Users/wangany/.m2/repository/org/springframework/boot/spring-boot-starter-jdbc/1.3.5.RELEASE/spring-boot-starter-jdbc-1.3.5.RELEASE.jar:/Users/wangany/.m2/repository/org/apache/tomcat/tomcat-jdbc/8.0.33/tomcat-jdbc-8.0.33.jar:/Users/wangany/.m2/repository/org/apache/tomcat/tomcat-juli/8.0.33/tomcat-juli-8.0.33.jar:/Users/wangany/.m2/repository/org/hibernate/hibernate-entitymanager/4.3.11.Final/hibernate-entitymanager-4.3.11.Final.jar:/Users/wangany/.m2/repository/org/jboss/logging/jboss-logging-annotations/1.2.0.Beta1/jboss-logging-annotations-1.2.0.Beta1.jar:/Users/wangany/.m2/repository/org/hibernate/hibernate-core/4.3.11.Final/hibernate-core-4.3.11.Final.jar:/Users/wangany/.m2/repository/org/jboss/jandex/1.1.0.Final/jandex-1.1.0.Final.jar:/Users/wangany/.m2/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar:/Users/wangany/.m2/repository/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2.jar:/Users/wangany/.m2/repository/org/hibernate/common/hibernate-commons-annotations/4.0.5.Final/hibernate-commons-annotations-4.0.5.Final.jar:/Users/wangany/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar:/Users/wangany/.m2/repository/org/javassist/javassist/3.18.1-GA/javassist-3.18.1-GA.jar:/Users/wangany/.m2/repository/javax/transaction/javax.transaction-api/1.2/javax.transaction-api-1.2.j
    ```
    86322989
        5
    86322989  
    OP
       2017-11-08 16:31:47 +08:00
    16:29:15.288 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default
    16:29:15.314 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
    16:29:15.316 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://localhost:8090][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
    16:29:15.346 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://localhost:8090][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
    16:29:15.349 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {}->http://localhost:8090
    16:29:15.369 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to localhost/127.0.0.1:8090
    16:29:15.382 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connection established 127.0.0.1:60045<->127.0.0.1:8090
    16:29:15.382 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Executing request GET /robots.txt HTTP/1.1
    16:29:15.382 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
    16:29:15.384 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> GET /robots.txt HTTP/1.1
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0) Gecko/20100101 Firefox/56.0
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip, deflate
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: keep-alive
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Upgrade-Insecure-Requests: 1
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Pragma: no-cache
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Cache-Control: no-cache
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Cookie: JSESSIONID=1DC5225AB09DC08E0A39366DFD40384B; user=DEFAULT_zhangsan
    16:29:15.389 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: localhost:8090
    16:29:15.389 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "GET /robots.txt HTTP/1.1[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0) Gecko/20100101 Firefox/56.0[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip, deflate[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: keep-alive[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Upgrade-Insecure-Requests: 1[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Pragma: no-cache[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Cache-Control: no-cache[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Cookie: JSESSIONID=1DC5225AB09DC08E0A39366DFD40384B; user=DEFAULT_zhangsan[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: localhost:8090[\r][\n]"
    16:29:15.390 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
    16:29:15.391 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 405 Method Not Allowed[\r][\n]"
    16:29:15.391 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Server: GCDWebServer[\r][\n]"
    16:29:15.392 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Connection: Close[\r][\n]"
    16:29:15.392 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Wed, 08 Nov 2017 08:29:15 GMT[\r][\n]"
    16:29:15.392 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
    16:29:15.402 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 405 Method Not Allowed
    16:29:15.402 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: GCDWebServer
    16:29:15.402 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Connection: Close
    16:29:15.402 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Wed, 08 Nov 2017 08:29:15 GMT
    16:29:15.415 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: Close connection
    Disconnected from the target VM, address: '127.0.0.1:60042', transport: 'socket'
    16:29:15.417 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Connection discarded
    16:29:
    86322989
        6
    86322989  
    OP
       2017-11-08 17:10:43 +08:00
    把 firebug 下的 header 全加上去也不行.(草你妈而隔壁)

    request.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0) Gecko/20100101 Firefox/56.0");
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    request.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
    request.addHeader("Accept-Encoding", "gzip, deflate");
    request.addHeader("Connection", "keep-alive");
    request.addHeader("Upgrade-Insecure-Requests", "1");
    request.addHeader("Pragma", "no-cache");
    request.addHeader("Cache-Control", "no-cache");
    request.addHeader("Cookie", "JSESSIONID=1DC5225AB09DC08E0A39366DFD40384B; user=DEFAULT_zhangsan");
    86322989
        7
    86322989  
    OP
       2017-11-08 17:11:22 +08:00
    v 站不能删帖.
    草拟吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2945 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:40 · PVG 22:40 · LAX 06:40 · JFK 09:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.