- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试向需要 cookie 的网页发出请求。我正在使用 HTTPUrlConnection,但响应总是返回
<div class="body"><p>Your browser's cookie functionality is turned off. Please turn it on.
我怎样才能发出请求,使被查询的服务器认为我打开了 cookie。我的代码是这样的。
private String readPage(String page) throws MalformedURLException {
try {
URL url = new URL(page);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.connect();
InputStream in = uc.getInputStream();
int v;
while( (v = in.read()) != -1){
sb.append((char)v);
}
in.close();
uc.disconnect();
} catch (IOException e){
e.printStackTrace();
}
return sb.toString();
}
最佳答案
您需要在系统中添加一个CookieHandler 来处理cookie。 Java 6之前,JRE中没有CookieHandler的实现,得自己写。如果你使用的是 Java 6,你可以这样做,
CookieHandler.setDefault(new CookieManager());
URLConnection 的 cookie 处理非常薄弱。它几乎不起作用。它没有正确处理所有 cookie 规则。如果您要处理身份验证等敏感 cookie,则应使用 Apache HttpClient。
关于java - 使用 Java URLConnection 关闭 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1455856/
在我的程序中,我需要跟踪已打开的与某些 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()
我是一名优秀的程序员,十分优秀!