简体   繁体   English

尝试读取响应正文时获取服务器响应 405

[英]Getting server response 405 when trying to read response body

I am trying to read the response body for a webservice request which is a POST method.我正在尝试读取作为 POST 方法的 webservice 请求的响应正文。

I have tried changing the method type to GET just to try even though it is a POST request it did not work.我尝试将方法类型更改为 GET 只是为了尝试,即使它是一个 POST 请求,但它不起作用。 However, it does give me a response code '200' yet while trying to read the response body with a reader i get error '405'.但是,它确实给了我一个响应代码“200”,但在尝试使用阅读器读取响应正文时,我收到错误“405”。

Could anyone please give me some guidance on this.任何人都可以请给我一些指导。 I need to read the entire response body and then save one item which is a dynamic alphanumeric string to another property file.我需要读取整个响应正文,然后将一个动态字母数字字符串的项目保存到另一个属性文件中。

Thanks in advance.提前致谢。

` `

try{
    URL url = new URL(myUrl);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod(POST);
    conn.connect();
    int respCode = conn.getResponseCode();
    if(respCode != 200)
    {
        System.out.println("connection refused");
    }
    else{
        Scanner sc = new Scanner(url.openStream());
        while(sc.hasNext()){
            inline = sc.nextLine();
            System.out.println(inline);
            sc.close();
        }
}catch(Exception e){
e.prinstackTrace();
}

` `

Response Codes which are in the form of 4xx are client error codes. 4xx 形式的响应代码是客户端错误代码。 Normally, 405 would mean "Method Not Allowed" but I have seen them sometimes when the client is unable to understand the type of response as well.通常,405 表示“不允许的方法”,但有时当客户端也无法理解响应类型时,我会看到它们。

You should add headers - Content-Type and Accept in your request and then see the output.您应该在请求中添加标头 - Content-Type 和 Accept,然后查看输出。

It might solve your problem.它可能会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM