简体   繁体   English

如何在Android Java中发出相同的cURL请求?

[英]How to make same cURL request in android java?

I cannot understand request below for cURL: 我无法理解以下有关cURL的请求:

    curl -X POST -u "client_id:secret" \
  https://bitbucket.org/site/oauth2/access_token -d grant_type=password \
  -d username={username} -d password={password}

How can I make same request in java (Android)? 如何在Java(Android)中发出相同的请求? Now, I try so: 现在,我尝试这样做:

String auth = vcs.getKey() + ":" + vcs.getSecret();
byte[] authEncBytes = Base64.encode(auth.getBytes(), Base64.DEFAULT);
String authStringEnc = new String(authEncBytes);
URL url = new URL("https://bitbucket.org/site/oauth2/access_token");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestMethod("POST");
connection.setRequestProperty("grant_type", "password");
connection.setRequestProperty("username", mLogin);
connection.setRequestProperty("password", mPassword);

But it doesn't work. 但这是行不通的。

You are passing the post parameters with at request header. 您正在传递带有at请求标头的post参数。 Remove the last three and replace with below: 删除最后三个,并替换为以下内容:

String params = "grant_type=password&username=xyz&password="+ java.net.URLEncoder.encode("urp&^!ass", "UTF-8");
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8"));
writer.write(params);
writer.flush();
writer.close();
os.close();
int responseCode = connection.getResponseCode();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM