java 在Eclipse中调用RestAPI时收到400个错误请求,但在Postman中工作

dkqlctbz  于 2023-02-07  发布在  Java
关注(0)|答案(1)|浏览(221)

我有一个函数接受2输入-

  1. JSON文件路径
    1.复位URL
    调用函数后,它应该返回JSON形式的输出。
    但这里的问题,它是抛出400坏请求错误.下面是我得到的错误日志-
Error Log :
URL:
http://localhost:26081/jderest/v3/orchestrator/ORC_2212160001JDE
Conn : sun.net.www.protocol.http.HttpURLConnection:http://localhost:26081/jderest/v3/orchestrator/ORC_2212160001JDE
JSON Input path : \\phxxxx155826.xxxxxx.fusionappsdphx1.xxxxxxxx.com\\FF\\Orchest\\orc1.json
{
"username": "ic8823790",
"password": "ic8823790",
"Product_No": "sss265",
"Description": "sss265",
"Stocking_Type": "s",
"G_L_Class": "in30",
"Unit_of_Measure": "ea",
"Search_Text": "sss265",
"Branch_Plant": "30"
}
HTTP CODE : 400
java.io.IOException: Server returned HTTP response code: 400 for URL: http://exx11001.ad1.xxxxxxxdephx.xxxxxxx.com:26081/jderest/v3/orchestrator/ORC_2212160001JDE
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1944)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1939)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1938)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1508)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at OrchestratorAPI.orchestratorCall(OrchestratorAPI.java:108)
at OrchestratorAPI.main(OrchestratorAPI.java:25)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://ems11001.ad1.fusionappsdephx.oraclevcn.com:26081/jderest/v3/orchestrator/ORC_2212160001JDE
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at OrchestratorAPI.orchestratorCall(OrchestratorAPI.java:100)
... 1 more

这是我所尝试的来源-

public class OrchestratorAPI {

    public static void main(String[] args) throws MalformedURLException, IOException {
        // TODO Auto-generated method stub
        String filepath = "\\\\xxxxxxx155826.xxxxxxx.fusionappsdphx1.xxxxxxx.com\\FF\\Orchest\\orc1.json";
        String orchAPI = "http://exx11001.ad1.xxxxxxxdephx.xxxxxxx.com:26081/jderest/v3/orchestrator/ORC_2212160001JDE";
        orchestratorCall(filepath, orchAPI);
    
    }
    
    public static void orchestratorCall(String filePath, String orchestAPI) throws MalformedURLException, IOException {
    
        String charset = "UTF-8";
        StringBuilder result;
        String content = "";
        String path = "", sid = "";
        String paramsJSON="";
    
        URLConnection con = new URL(orchestAPI).openConnection();
        System.out.println("URL : "+orchestAPI + "\nConn : "+con);
    
        System.out.println("JSON Input path : " + filePath);
        try {
            paramsJSON = new String(
                Files.readAllBytes(Paths.get(filePath)));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    
        System.out.println(paramsJSON);
    
        try {
            ((HttpURLConnection) con).setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json; UTF-8");
            con.setRequestProperty("Accept", "application/json");
    
            con.setDoOutput(true);
            con.setReadTimeout(60000);
            con.setConnectTimeout(60000);
    
            try (OutputStream os = con.getOutputStream()) {
                byte[] input = paramsJSON.getBytes(charset);
                os.write(input, 0, input.length);
            }
    
            int code = ((HttpURLConnection) con).getResponseCode();
            System.out.println("HTTP CODE : " + String.valueOf(code));
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        try {
            // Receive the response from the server
            InputStream in = new BufferedInputStream(con.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            result = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
    
            System.out.println("JSON Parser -> result: " + result.toString());
    
            try {
                // Create new report file
                content = result.toString();
                path = "\\\\xxxxxxx155826.xxxxxxx.fusionappsdphx1.xxxxxxxx.com\\FF\\Orchest\\" + "" + "OrchestratorReport.json";
                System.out.println("Home Dir path : " + path);
                File file = new File(path);
    
                // If file doesn't exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }
    
                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
    
                // Write in file
                bw.write(content);
    
                // Close connection
                bw.close();
            } catch (Exception e) {
                System.out.println(e);
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        ((HttpURLConnection) con).disconnect();   
    }
}
4jb9z9bj

4jb9z9bj1#

问题解决了,我刚刚把UTF-8从连接的setRequestProperty上去掉了,现在可以工作了。
之前:

con.setRequestProperty("Content-Type", "application/json; UTF-8");

之后:

con.setRequestProperty("Content-Type", "application/json");

相关问题