[英]curl command execution in java
這是我試圖在Java中執行的curl命令:
curl -XPOST \
https://login.spredfast.com/v1/oauth/authorize \
-d response_type="code" \
-d state="<origState>" \
--data-urlencode password="<userPassword>" \
--data-urlencode client_id="<clientId>" \
--data-urlencode email="<userEmail>" \
--data-urlencode redirect_uri="<redirectUri>"
這是我上面的java程序:
package jsontocsv;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class NoName2 {
public static void main(String[] args) {
NoName2 obj = new NoName2();
String[] command = new String[]
{
"curl","-XPOST", "https://login.xyz.com/v1/oauth/authorize",
"-d", "'response_type=code'",
"-d", "'state=none'",
"--data-urlencode","'password=<password>'",
"--data-urlencode", "'client_id=<client id>'",
"--data-urlencode", "'email=<email>'",
"--data-urlencode", "'redirect_uri=https://localhost'",
};
String output = obj.executeCommand(command);
System.out.println(output);
}
private 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()));
System.out.println(reader.readLine()); // value is NULL
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
但是我得到的輸出不是我期望的。 似乎curl命令的高亮顯示的行似乎沒有在運行:
"--data-urlencode","'password=<password>'",
"--data-urlencode", "'client_id=<client id>'",
"--data-urlencode", "'email=<email>'",
"--data-urlencode", "'redirect_uri=https://localhost'",
我的curl命令及其參數的代碼格式正確嗎? 任何幫助深表感謝! 提前致謝!
我強烈建議您為此使用HTTP庫,並避免執行外部程序。 那里有許多用於Java的HTTP庫(用於Java的Rest客戶嗎? )。
您一定應該看看Retrofit,在我看來,這非常方便( http://square.github.io/retrofit/ )。
您可能還想使用OkHTTP或AsyncHTTPClient。
后者解決您的問題的示例:
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
BoundRequestBuilder r = asyncHttpClient.preparePost("https://login.xyz.com/v1/oauth/authorize");
r.addParameter("password", "<value>");
r.addParameter("client_id", "<id>");
r.addParameter("email", "<email>");
r.addParameter("redirect_uri", "https://localhost");
Future<Response> f = r.execute();
Response r = f.get();
然后,響應對象提供狀態代碼或HTTP正文。 ( https://asynchttpclient.github.io/async-http-client/apidocs/com/ning/http/client/Response.html )
編輯:
有點奇怪,是您要發布,但是說使用curl到url編碼您的參數,這在使用HTTP Post時並不常見,也許您可以嘗試:
curl -XPOST \
https://login.spredfast.com/v1/oauth/authorize \
-d response_type="code" \
-d state="<origState>" \
--data 'password="<userPassword>"' \
--data 'client_id="<clientId>"' \
--data 'email="<userEmail>"' \
--data 'redirect_uri="<redirectUri>"'
編輯:完整示例
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Response;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
AsyncHttpClient.BoundRequestBuilder r = asyncHttpClient.preparePost("https://httpbin.org/post");
r.addFormParam("password", "<value>");
r.addFormParam("client_id", "<id>");
r.addFormParam("email", "<email>");
r.addFormParam("redirect_uri", "https://localhost");
Future<Response> f = r.execute();
Response res = f.get();
System.out.println(res.getStatusCode() + ": " + res.getStatusText());
System.out.println(res.getResponseBody());
}
}
輸出:
200: OK
{
"args": {},
"data": "",
"files": {},
"form": {
"client_id": "<id>",
"email": "<email>",
"password": "<value>",
"redirect_uri": "https://localhost"
},
"headers": {
"Accept": "*/*",
"Content-Length": "94",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "AHC/1.0"
},
"json": null,
"origin": "??.??.??.??",
"url": "https://httpbin.org/post"
}
您可以使用以下Maven添加AsyncHTTPClient庫( http://search.maven.org/#artifactdetails%7Ccom.ning%7Casync-http-client%7C1.9.36%7Cjar ):
<dependency>
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
<version>1.9.36</version>
</dependency>
通常,只需看看Java的不同HTTP客戶端庫,然后使用您最喜歡的庫即可(我更喜歡前面提到的Retrofit)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.