首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复java中的http响应代码400错误?是否存在格式错误的请求语法或无效的请求消息框架?

如何修复java中的http响应代码400错误?是否存在格式错误的请求语法或无效的请求消息框架?
EN

Stack Overflow用户
提问于 2019-05-06 19:26:09
回答 1查看 810关注 0票数 2

我在into中创建了一个ML模型,并将模型部署到web服务中。我需要创建一个java程序,它发送输入并从web服务中检索输出。

我检查了我所有的连接凭证,没有问题。

我在这里使用的代码与watson-studio (部署部分下的implementation选项卡)中给出的代码相同,但我仍然会出错。

程序在这一行停止。

代码语言:javascript
复制
scoringBuffer = new BufferedReader(new InputStreamReader(scoringConnection.getInputStream()));

检查这个代码:-

代码语言:javascript
复制
package Original;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class Iris_Deployment {

    public static void main(String[] args) {
        // NOTE: you must manually construct wml_credentials hash map below
        // using information retrieved from your IBM Cloud Watson Machine Learning Service instance.

        Map<String, String> wml_credentials = new HashMap<String, String>()
        {{
            put("url", "https://eu-gb.ml.cloud.ibm.com");
            put("username", "my-username-comes-here");
            put("password", "my-password-comes-here");
        }};

        String wml_auth_header = "Basic " +
                Base64.getEncoder().encodeToString((wml_credentials.get("username") + ":" +
                        wml_credentials.get("password")).getBytes(StandardCharsets.UTF_8));
        String wml_url = wml_credentials.get("url") + "/v3/identity/token";
        HttpURLConnection tokenConnection = null;
        HttpURLConnection scoringConnection = null;
        BufferedReader tokenBuffer = null;
        BufferedReader scoringBuffer = null;
        try {
            // Getting WML token
            URL tokenUrl = new URL(wml_url);
            tokenConnection = (HttpURLConnection) tokenUrl.openConnection();
            tokenConnection.setDoInput(true);
            tokenConnection.setDoOutput(true);
            tokenConnection.setRequestMethod("GET");
            tokenConnection.setRequestProperty("Authorization", wml_auth_header);
            tokenBuffer = new BufferedReader(new InputStreamReader(tokenConnection.getInputStream()));
            StringBuffer jsonString = new StringBuffer();
            String line;
            while ((line = tokenBuffer.readLine()) != null) {
                jsonString.append(line);
            }

            // Scoring request
            URL scoringUrl = new URL("https://eu-gb.ml.cloud.ibm.com/v3/wml_instances/3c523ca2-c5b5-409f-9773-7634c8bc5144/deployments/80b8046d-84e0-4081-a770-d10099c4a378/online");
            String wml_token = "Bearer " +
                    jsonString.toString()
                            .replace("\"","")
                            .replace("}", "")
                            .split(":")[1];
            scoringConnection = (HttpURLConnection) scoringUrl.openConnection();
            scoringConnection.setDoInput(true);
            scoringConnection.setDoOutput(true);
            scoringConnection.setRequestMethod("POST");
            scoringConnection.setRequestProperty("Accept", "application/json");
            scoringConnection.setRequestProperty("Authorization", wml_token);
            scoringConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            OutputStreamWriter writer = new OutputStreamWriter(scoringConnection.getOutputStream(), "UTF-8");

            // NOTE: manually define and pass the array(s) of values to be scored in the next line
            String payload = "{\"fields\": [\"SepalLengthCm\", \"SepalWidthCm\", \"PetalLengthCm\", \"PetalWidthCm\"], \"values\": [{1.2, 1.3, 2.2, 2.3}]}";
            writer.write(payload);
            writer.close();

            scoringBuffer = new BufferedReader(new InputStreamReader(scoringConnection.getInputStream()));
            StringBuffer jsonStringScoring = new StringBuffer();
            String lineScoring;
            while ((lineScoring = scoringBuffer.readLine()) != null) {
                jsonStringScoring.append(lineScoring);
            }
            System.out.println(jsonStringScoring);
        } catch (IOException e) {
            System.out.println("The URL is not valid.");
            e.printStackTrace();
        }
        finally {
            if (tokenConnection != null) {
                tokenConnection.disconnect();
            }
            if (tokenBuffer != null) {
                try {
                    tokenBuffer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (scoringConnection != null) {
                scoringConnection.disconnect();
            }
            if (scoringBuffer != null) {
                try {
                    scoringBuffer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

错误:-

代码语言:javascript
复制
The URL is not valid.
java.io.IOException: Server returned HTTP response code: 400 for URL: https://eu-gb.ml.cloud.ibm.com/v3/wml_instances/3c523ca2-c5b5-409f-9773-7634c8bc5144/deployments/80b8046d-84e0-4081-a770-d10099c4a378/online
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245)
    at Original.Iris_Deployment.main(Iris_Deployment.java:71)

Process finished with exit code 0
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-07 20:15:12

请试着按以下方式传递您的得分有效载荷:

\“数值\”:[1.2、1.3、2.2、2.3]

(将内部"{“替换为"[")

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56011224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档