从Eclipse发送时curl请求不起作用,但通过命令行起作用

时间:2020-05-28 20:30:52

标签: eclipse curl token

这是我在eclipse上在Mac上运行以执行命令的代码。

String cmd = "curl -i -X POST <webaddress> -H 'Cache-Control: no-cache' -H 'Content-Type: application/json' -H 'Postman-Token: <Postman-token>' -d '{ \"Username\":\"<username>\", \"Password\":\"<password\" }'";

System.out.println(MyUtils.executeCommand(cmd));

这是方法MyUtils.executeCommand。

public static String executeCommand(String command) {
    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader = 
                        new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line = "";           
        while ((line = reader.readLine())!= null) {
            //if(line.startsWith("authorization:")){
                output.append(line + "\n");
            //}
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return output.toString();
}

从Eclipse运行时,我得到:

date: Thu, 28 May 2020 20:09:53 GMT
content-type: application/json; charset=utf-8
content-length: 152
cache-control: no-cache
pragma: no-cache
expires: -1
server: Microsoft-IIS/10.0
x-powered-by: ASP.NET

{"errorCode":0,"Title":"Error logging in.","Description":"There was an issue with the login data that was provided.  Please try again.","ShowCTA":false}

从Mac终端运行时,获得顶部和授权代码。我不确定为什么日食会有所不同。我需要更改eclipse中的某些配置,还是在阻止eclipse的服务器端有安全性?

我可以从Eclipse中运行其他命令。当输入String为“ ls”或“ curl http://www.tutorialspoint.com -i”时,我从eclipse中获得的结果与从终端获得的结果相同。

0 个答案:

没有答案