- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 URLConnection 连接到主服务器。服务器实现摘要认证。如果我使用 java 库连接到服务器,则连接成功。但是,如果我对 android 使用相同的代码,连接将被拒绝,原因是 - 用户名和密码不匹配。
这是我的 Java 项目的代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
public class Connect {
static StringBuilder sb;
public static void main(String args[]) {
String strURL = "http://hostserver/";
final String username = "username";
final String password = "password";
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray());
System.out.println(pa.getUserName() + ":" + new String(pa.getPassword()));
return pa;
}
});
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(strURL);
URLConnection connection = url.openConnection();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
} catch (java.net.ProtocolException e) {
sb.append("User Or Password is wrong!");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
System.out.println("Exception");
}
}
System.out.println("The Data is: " + sb.toString());
}
}
上面的代码工作正常,我能够连接到我的主机服务器,它正在实现摘要式身份验证。我无法使用与 Android 相同的代码进行连接。这是我的安卓代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Connect extends Activity {
static StringBuilder sb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String strURL = "http://hostserver/";
final String username = "username";
final String password = "password";
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray());
// System.out.println(pa.getUserName() + ":" + new String(pa.getPassword()));
return pa;
}
});
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(strURL);
URLConnection connection = url.openConnection();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
} catch (java.net.ProtocolException e) {
sb.append("User Or Password is wrong!");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
System.out.println("Exception");
}
}
Log.d("DATA", sb.toString());
}
}
如果相同的代码适用于 Java,那么它也应该适用于 Android。
代码在 Authenticator 中循环,因为它发现用户名和密码由于某种原因在 Android 代码中不匹配,而实际上是正确的。该代码非常适合 Java 项目。
最佳答案
while ((line = in.readLine()) != null) {
sb.append(line);
}
去掉这段代码,只写
line = in.readLine().toString();
sb.append(line);
关于android - UrlConnection 使用 Java 代码但不适用于 Android - 在 Digest Authenticated Server 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3197814/
在我的程序中,我需要跟踪已打开的与某些 HTTP 服务器的连接列表 - 以便在需要时立即断开连接。 我遇到了以下问题。如果我连接到 HTTP 服务器,一切正常,但如果连接到 HTTPS,则连接不会从列
我正在尝试写信给 URLConnection#getOutputStream ,但是,在我调用 URLConnection#getInputStream 之前,实际上并没有发送任何数据。 .即使我设置
我有一组对象,我试图通过 POST 发送到 API。第一个对象将按原样通过,然后我得到: java.net.ProtocolException: cannot write request body a
我在尝试发送文本时遇到 URLConnection 编码问题。 我的代码是这样的: final URL url = new URL(urlString); final URLConnection ur
我有这种方法,可以从雅虎财经下载 .csv 文件并将其保存在本地。它是在循环期间访问的,因此它从列表中下载许多文件。然而,有时符号输入不正确、不再存在或连接超时。如何修改此方法,以便重试连接超时并跳过
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import
打开 URLConnection 时,我使用以下代码来获取内容长度,但它返回 -1。 URL url = new URL(sUrl[0]); URLConnection connection = ur
默认情况下,URLConnection 的超时为 0,这是无限制的。 XXXXX 的合理值是多少? URL url = ... URLConnection uCon = url.openConnect
我无法打开具有特定网络资源的 URLConnection。我得到了 “java.net.ConnectException:连接超时:”。是因为该域阻止了直接 URL 连接吗?如果是这样,他们是如何阻止
我看过文本文件示例,但是否以与 URLConnection 相同的方式将音频文件直接保存到服务器? 最佳答案 是的,一样的。尽管确保使用二进制输出流将内容写入磁盘。 类似于: URLConne
我一直在尝试从网页获取信息,特别是此网站:http://www.ncbi.nlm.nih.gov/pubmed?term=%22pulmonary%20disease%2C%20chronic%20o
在我使用 Apache 库 (org.apach.httpclient) 向带有参数 (BasicNameValuePair) 的 Php 脚本发出请求之前, 然后现在我想删除那些库以减小 APK 大
我正在使用 android 应用程序,我正在从 url 下载文件。一切正常,但是当互联网连接介于两者之间(打开连接后)时,下载超时永远不会发生并且连接永远不会结束。 给我一个解决这个问题的方案
我只是想了解一下 JacksonJson 库。为此,我尝试将 Places API 中的 JSON 数据转换为字符串。 我的 key 有效(我在浏览器和另一个应用程序中进行了测试),但出现错误。这是代
我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用 curl
HttpsURLConnection 有问题 - 未使用代理。 这是代码: //proxy String type = "https"; System.getProperties().put(type
我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写一个 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用cur
在我的应用程序中,我需要下载一些网页。我是这样做的 URL url = new URL(myUrl); HttpURLConnection conn = (HttpURLConnection) url
我正在尝试使用 URLConnection 获得最低级别的字节计数.我已经用 CountingInputStream 计算了两个流传递的数据和 CountingOutputStream来自 Apach
我正在使用这段代码创建一个常规的 HTTP 连接: URLConnection cn = new URL( "http://...." ).openConnection(); cn.connect()
我是一名优秀的程序员,十分优秀!