始终状态代码即将到来401

时间:2015-06-04 13:33:36

标签: android basic-authentication multipartform-data

我正在使用基本身份验证进行一次表单发布,但状态代码始终为401。

  HttpClient httpClient = new DefaultHttpClient();
  HttpPost httpPost = new HttpPost(url);

  MultipartEntityBuilder builder = MultipartEntityBuilder.create();        
  builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

  builder.addPart("picture", new FileBody(new File(picture)));
  builder.addTextBody("firstName", firstname);
  builder.addTextBody("lastName", lastname);

  String base64EncodedCredentials = "Basic "   +Base64.encodeToString((username+":"+password).getBytes(), Base64.NO_WRAP);
  Log.d("Authorization", base64EncodedCredentials);

  httpPost.setHeader("Authorization", base64EncodedCredentials);
  httpPost.setEntity(builder.build());

  HttpResponse httpResponse = httpClient.execute(httpPost);
  HttpEntity httpEntity = httpResponse.getEntity();

但是它在浏览器中完美运行(Advance rest client),请仔细阅读我的代码并建议我解决一些问题。

2 个答案:

答案 0 :(得分:0)

使用Feedler等工具将您发送的http请求与PostMan发送的请求进行比较,看看您缺少的内容

答案 1 :(得分:0)

试试这个

String base64EncodedCredentials = "Basic "   +Base64.encodeToString((username+":"+password).getBytes("UTF-8"), Base64.DEFAULT);

而不是

String base64EncodedCredentials = "Basic "   +Base64.encodeToString((username+":"+password).getBytes(), Base64.NO_WRAP);