java - 身份验证问题..!!在 Android 上使用 HTTPURLCONNECTION

标签 java android http http-status-codes

"基本YWRtaW46YW RtaW4=" 是我地址的正确代码。我检查 j2me 项目。

在 Android 上,我的 getbase64 方法返回“Basic YWRtaW46YW RtaW4="这是真的。

我将它用于:

httpConnection.setRequestProperty("授权",getBase64Encode());

最后响应码是401

有什么想法吗???

protected void connect() {
  InputStream is = null;
  OutputStream os = null;
  try {

   url = new URL(getUrl());
   System.out.println(getUrl());// duzelt
   queryString = encodeURL(queryString);
   byte postmsg[] = queryString.getBytes("UTF-8");
   conn = url.openConnection();
   if (!(conn instanceof HttpURLConnection))
    throw new IOException("Not an HTTP connection");

   httpConnection = (HttpURLConnection) conn;
   HttpOptions options=new HttpOptions();
   httpConnection.setDoInput(true);
   httpConnection.setDoOutput(true);
   httpConnection.setRequestMethod("GET");
   httpConnection.setRequestProperty("Authorization",
     getBase64Encode());
   httpConnection.connect();
   os = httpConnection.getOutputStream();// ///////////////////baglantının
   System.out.println("response code: "+ httpConnection.getResponseCode());
   // connect olup olmadıgını
   // kontrol et

   for (int i = 0; i < postmsg.length; i++) {
    os.write(postmsg[i]);
   }
   if (!cancel) {
    onReturn(httpConnection.getResponseCode(), httpConnection
      .getInputStream());
   }
   os.close();
   // httpConnection.close();
  } catch (Exception e) {
   System.out.println(e.getMessage());
   try {
    httpConnection.disconnect();
    Thread.sleep(60);
    // cancel=true; eklenmesı gerekebilir
   } catch (Exception ie) {
   }
   onError(e);
  }

最佳答案

有一个setConnectTimeout(int) HttpURLConnection 上的方法。

Sets the timeout value in milliseconds for establishing the connection to the resource pointed by this URLConnection instance. A SocketTimeoutException is thrown if the connection could not be established in this time. Default is 0 which stands for an infinite timeout.

httpConnection.setConnectTimeout(10000); /* connection timeout set to 10s */

关于java - 身份验证问题..!!在 Android 上使用 HTTPURLCONNECTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3943130/

相关文章:

http - 将 GET 与 POST 混合使用 - 这是一种不好的做法吗?

java - 使用现有 EJB 服务从 Tapestry 执行长时间运行的任务

java - 带有 Recyclerview 的 Exoplayer

android - 在Android中对ImageView应用连续的动画

javascript - Titanium Android 应用程序在更新 ScrollableView 时崩溃

android - shouldOverrideUrlLoading() 永远不会被调用

node.js - 从 Node HTTP 请求到外部 API 中删除端口号

javascript - 如何验证我所有的 (JavaScript) AJAX 请求? (发送到 Node.js)

java - 如何在 Java Jersey Application 中使用查询参数?

java - java/gwt UI编码-干净的代码