java - Java 处理 cURL 的方式不同吗?
问题描述
我正在尝试执行 POST 请求curl
,以便进行一些授权。预期结果是一个令牌,将在Location response header
问题
- 如果我从我的 bash 中执行命令,它会工作得很好(= 使用重定向等)
- 如果我在我的应用程序中执行完全相同的命令(实际上只是复制和粘贴)
Java
,我会得到稍微不同的结果(= 中缺少令牌Location response header
)
预期结果
令牌位于Location response header
, likeLocation: /https://localhost:4200/token=123
实际结果
某种错误,例如Location: my.policy
命令
这是命令的基本结构。基本上,它是一个涉及一些重定向的 POST 请求。
curl -L -v --header \"something:special\” --cookie \"a=1;b=2\" --data \"param1¶m2\"
https://网址
我试过的
- 通过启用详细模式
-v
- 通过启用重定向
-L
- 检查握手(或常见)错误
- 检查错别字和编码问题
- 内执行的命令
bash
- 尝试使用
Java
基于方法执行命令 - 甚至尝试执行
bash
从代码中创建一个并传递命令的命令
Java 代码
String command = "SOME CURL COMMAND USING POST"
// execute the command
Process process = Runtime.getRuntime().exec(command);
// Since we will get a 302 (as expected), the result will be in the error stream
Scanner scanner = new Scanner(process.getErrorStream());
// Do some formatting...
String tmp = "";
while (scanner.hasNext()) {
String t = scanner.next();
if (t.equals("*") || t.equals("<") || t.equals(">")) {
tmp += "\n";
}
tmp += t + " ";
}
System.out.println("Content" + tmp);
问题
Java 是否以不同的方式处理执行?因为我不明白为什么我会得到不同的结果
任何帮助表示赞赏。
最好的
解决方案
推荐阅读
- javascript - 为什么我收到错误的 javascript 消息
- java - testng.xml 的批处理文件未按计划运行
- c# - 为什么这个 linq SQL 查询的错误嵌套太深?
- php - Facebook Socialite 从令牌和秘密中获取用户
- r - 将 geom_tile 用于热图
- denodo - 在 Denoto 中使用日期变量在分隔文件基本视图上获取“插值错误”
- node.js - 将 Watson api 连接到客户端服务器
- c# - 从一个控制器将 JSON 数据发布到多个模型/表中
- chromium - 可以在系统上安装多个版本的快照吗?
- javascript - 如何在javascript中的数组内每5分钟调用一个函数