Java中使用HttpURLConnection的POST



我在使用HttpURLConnection而不是Apache库时遇到了一些问题,以下用于加载令牌的代码运行良好

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import Decoder.BASE64Encoder;
private void LoadToken(String username, String password, String url) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(
password.getBytes(),
"HmacMD5");
String computedHashString = "";
try {
Mac mac = Mac.getInstance("HmacMD5");
mac.init(keySpec);
byte[] result = mac.doFinal(url.getBytes());
BASE64Encoder encoder = new BASE64Encoder();
computedHashString = encoder.encode(result); 
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("Can not create token (NoSuchAlgorithmException)");
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("Can not create token (InvalidKeyException)");
}
HttpPost httpPost = new HttpPost(url);  
httpPost.setHeader("Authorization", "Bearer " + username + ":" + computedHashString);
try {
CloseableHttpResponse response = httpclient.execute(httpPost);
ObjectMapper objectMapper = new ObjectMapper();
if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
RetrieveException(response, objectMapper);
}
AccessToken accessToken = objectMapper.readValue(response.getEntity().getContent(), AccessToken.class);
token = accessToken;
} 
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("Can not create token (ClientProtocolException)");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("Can not create token (IOException)");
}
}

然而,当我尝试使用HttpURLConnection时(我需要这样做才能将其包含在android项目中(,如下所示:

private void LoadToken(String username, String password, String uri) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(
password.getBytes(),
"HmacMD5");
String computedHashString = "";
try {
Mac mac = Mac.getInstance("HmacMD5");
mac.init(keySpec);
byte[] result = mac.doFinal(uri.getBytes());
BASE64Encoder encoder = new BASE64Encoder();
computedHashString = encoder.encode(result); 
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("Can not create token (NoSuchAlgorithmException)");
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("Can not create token (InvalidKeyException)");
}
URL url = new URL(uri);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Authorization", "Bearer " + username + ":" + computedHashString);

try {
ObjectMapper objectMapper = new ObjectMapper();
AccessToken accessToken = objectMapper.readValue(urlConnection.getInputStream(), AccessToken.class);
token = accessToken;
} 
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("Can not create token (ClientProtocolException)");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("Can not create token (IOException)");
}
}

我得到服务器正在以状态代码411进行响应。如果这是一个愚蠢的问题,我很抱歉,但我从未使用过HttpURLConnection。我是不是错过了什么?我错在哪里了?提前感谢

设置urlConnection.setDoOutput(true)

相关内容

  • 没有找到相关文章

最新更新