- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在试验一个非常奇怪的问题。事实上,我几个月前制作了一个程序,最近它开始运行得不太好,所以我仔细观察了一下,发现 InputStream
输出有些奇怪。
我的程序需要从 Internet 获取一些页面,但有时(因为有时有效,有时无效...奇怪)我会得到像这样的奇怪字符:
有时我会得到好的 html 页面,但这种情况非常罕见。
代码是这样的(简单版)
InputStream is;
String inputLine;
if((is = URLConnectionReader.urlExists("xxxxurlxxxx")) != null)
{
BufferedReader in = null;
try
{
// First try
in = new BufferedReader(new InputStreamReader(is));
// Second try I don't have both in the code but both doesn't work 100% of the time.
in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
}
}
catch (Exception e) {
System.out.println("error");
}
}
我将 2 放入 = new() 中,因为我都尝试过但都没有成功。奇怪的是,有时会连续工作10次,然后连续10次不工作...
public static InputStream urlExists(String url)
{
try {
URL site = new URL(url);
try {
return site.openStream();
} catch (IOException ex) {
return null;
}
} catch (MalformedURLException ex) {
return null;
}
}
非常感谢您的帮助。
最佳答案
好的,我可以在这里重现该问题,但频率低于您所看到的。使用 tcpdump
跟踪流,我相信服务器已决定向您发送 gzip
压缩流,而不是未压缩流。
这是失败案例中 tcpdump
的相关 header 输出(第一行换行以便于阅读)。 Content-Encoding
在 0x024A
附近:
13:17:40.644241 IP r1.batoto.net.http > random.example.org.33779: Flags [P.],
seq 1:604, ack 183, win 122,
options [nop,nop,TS val 344850394 ecr 15945878], length 603
// Some lines of headers removed
0x0030: 0000 0000 4854 5450 2f31 2e31 2032 3030 ....HTTP/1.1.200
0x0040: 204f 4b0d 0a53 6572 7665 723a 206e 6769 .OK..Server:.ngi
0x0050: 6e78 2f31 2e32 2e37 0d0a 4461 7465 3a20 nx/1.2.7..Date:.
0x0060: 4672 692c 2032 3920 4e6f 7620 3230 3133 Fri,.29.Nov.2013
0x0070: 2030 323a 3437 3a33 3820 474d 540d 0a43 .02:47:38.GMT..C
0x0080: 6f6e 7465 6e74 2d54 7970 653a 2074 6578 ontent-Type:.tex
0x0090: 742f 6874 6d6c 3b63 6861 7273 6574 3d55 t/html;charset=U
0x00a0: 5446 2d38 0d0a 5472 616e 7366 6572 2d45 TF-8..Transfer-E
0x00b0: 6e63 6f64 696e 673a 2063 6875 6e6b 6564 ncoding:.chunked
0x00c0: 0d0a 436f 6e6e 6563 7469 6f6e 3a20 6b65 ..Connection:.ke
0x00d0: 6570 2d61 6c69 7665 0d0a 582d 506f 7765 ep-alive..X-Powe
0x00e0: 7265 642d 4279 3a20 5048 502f 352e 342e red-By:.PHP/5.4.
0x00f0: 3135 0d0a 5365 742d 436f 6f6b 6965 3a20 15..Set-Cookie:.
0x0100: 7365 7373 696f 6e5f 6964 3d32 3230 3433 session_id=22043
0x0110: 3838 3431 3235 3739 3735 6566 6361 3333 8841257975efca33
0x0120: 6462 3261 3264 6635 6665 643b 2070 6174 db2a2df5fed;.pat
0x0130: 683d 2f3b 2064 6f6d 6169 6e3d 2e62 6174 h=/;.domain=.bat
0x0140: 6f74 6f2e 6e65 743b 2068 7474 706f 6e6c oto.net;.httponl
0x0150: 790d 0a53 6574 2d43 6f6f 6b69 653a 2075 y..Set-Cookie:.u
0x0160: 6167 656e 745f 6279 7061 7373 3d31 3b20 agent_bypass=1;.
0x0170: 7061 7468 3d2f 3b20 646f 6d61 696e 3d2e path=/;.domain=.
0x0180: 6261 746f 746f 2e6e 6574 0d0a 5365 742d batoto.net..Set-
0x0190: 436f 6f6b 6965 3a20 636f 6d6d 656e 746d Cookie:.commentm
0x01a0: 6f64 7069 6473 3d64 656c 6574 6564 3b20 odpids=deleted;.
0x01b0: 6578 7069 7265 733d 5468 752c 2030 312d expires=Thu,.01-
0x01c0: 4a61 6e2d 3139 3730 2030 303a 3030 3a30 Jan-1970.00:00:0
0x01d0: 3120 474d 543b 2070 6174 683d 2f3b 2064 1.GMT;.path=/;.d
0x01e0: 6f6d 6169 6e3d 2e62 6174 6f74 6f2e 6e65 omain=.batoto.ne
0x01f0: 740d 0a43 6163 6865 2d43 6f6e 7472 6f6c t..Cache-Control
0x0200: 3a20 6d61 782d 6167 653d 3336 3030 0d0a :.max-age=3600..
0x0210: 4578 7069 7265 733a 2046 7269 2c20 3239 Expires:.Fri,.29
0x0220: 204e 6f76 2032 3031 3320 3033 3a34 373a .Nov.2013.03:47:
0x0230: 3338 2047 4d54 0d0a 5072 6167 6d61 3a20 38.GMT..Pragma:.
0x0240: 6e6f 2d63 6163 6865 0d0a 436f 6e74 656e no-cache..Conten
0x0250: 742d 456e 636f 6469 6e67 3a20 677a 6970 t-Encoding:.gzip
0x0260: 0d0a 5661 7279 3a20 4163 6365 7074 2d45 ..Vary:.Accept-E
0x0270: 6e63 6f64 696e 670d 0a58 2d43 6163 6865 ncoding..X-Cache
0x0280: 3a20 4849 540d 0a0d 0a37 6639 360d 0a :.HIT....7f96..
我不是 HTTP 协议(protocol)的 super 专家,但我的猜测是 gzip
Content-Encoding
不应该根据请求真正发生,并且是可能是服务器配置问题。也许其他比我更专业的人可以根据以下标题发表评论:
13:17:40.387277 IP random.example.org.33779 > r1.batoto.net.http:
Flags [P.], seq 1:183, ack 1, win 229,
options [nop,nop,TS val 15945878 ecr 344850135], length 182
// Some lines of headers removed
0x0030: 0000 0000 4745 5420 2f63 6f6d 6963 2f5f ....GET./comic/_
0x0040: 2f63 6f6d 6963 732f 6269 6c6c 792d 6261 /comics/billy-ba
0x0050: 742d 7237 3734 2048 5454 502f 312e 310d t-r774.HTTP/1.1.
0x0060: 0a55 7365 722d 4167 656e 743a 204a 6176 .User-Agent:.Jav
0x0070: 612f 312e 372e 305f 3435 0d0a 486f 7374 a/1.7.0_45..Host
0x0080: 3a20 7777 772e 6261 746f 746f 2e6e 6574 :.www.batoto.net
0x0090: 0d0a 4163 6365 7074 3a20 7465 7874 2f68 ..Accept:.text/h
0x00a0: 746d 6c2c 2069 6d61 6765 2f67 6966 2c20 tml,.image/gif,.
0x00b0: 696d 6167 652f 6a70 6567 2c20 2a3b 2071 image/jpeg,.*;.q
0x00c0: 3d2e 322c 202a 2f2a 3b20 713d 2e32 0d0a =.2,.*/*;.q=.2..
0x00d0: 436f 6e6e 6563 7469 6f6e 3a20 6b65 6570 Connection:.keep
0x00e0: 2d61 6c69 7665 0d0a 0d0a -alive....
好消息是修复相对简单。这是一个应该可以解决该问题的 urlExists(...)
实现:
private static final Object GZIP_CONTENT_TYPE = "gzip";
public static InputStream urlExists(String url)
{
try
{
URL site = new URL(url);
try
{
URLConnection conn = site.openConnection();
if (GZIP_CONTENT_TYPE.equals(conn.getContentEncoding()))
{
System.out.println("Using gzip stream");
return new GZIPInputStream(conn.getInputStream());
}
System.out.println("Using uncompressed stream");
return conn.getInputStream();
}
catch (IOException ex)
{
return null;
}
}
catch (MalformedURLException ex)
{
return null;
}
}
关于java - InputStream 给出奇怪的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20274548/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
为什么在 C# 中添加两个 char 结果是 int 类型? 例如,当我这样做时: var pr = 'R' + 'G' + 'B' + 'Y' + 'P'; pr 变量变为 int 类型。我希望它是
下面的代码可以编译,但 char 类型的行为与 int 类型的行为不同。 特别是 cout ::ikIsX >() ::ikIsX >() ::ikIsX >() using names
我正在寻找一个正则表达式,它可以匹配长度为 1 个或多个字符但不匹配 500 的内容。这将在 Rails 路由文件中使用,特别是用于处理异常。 路线.rb match '/500', to: 'err
对于 C 编程作业,我正在尝试编写几个头文件来检查所谓的“X 编程语言”的语法。我最近才开始,正在编写第一个头文件。这是我编写的代码: #ifndef _DeclarationsChecker_h_
为什么扩展的 ascii 字符(â、é 等)被替换为 字符? 我附上了一张图片...但我正在使用 PHP 从 MySQL 中提取数据,其中一些位置有扩展字符...我使用的是 Arial 字体。 您可以
我有一个与 R 中的断线相关的简单问题。 我正在尝试粘贴,但在获取(字符/数字)之间的断线时遇到问题。请注意,这些值包含在向量中(V1=81,V2=55,V3=25)我已经尝试过这段代码: cat(p
如何将 ANSI 字符 (char) 转换为 Unicode 字符 (wchar_t),反之亦然? 是否有用于此目的的任何跨平台源代码? 最佳答案 是的,在 中你有mbstowcs()和 wcsto
函数 fromCharCode 不适用于国际 ANSI 字符。例如,对于 ID 为 192 到 223 的俄语 ANSI (cp-1251) 字符,它返回特殊字符。如何解决这个问题? 我认为,需要将A
如果不喜欢,我想隐藏 id,但不起作用 SELECT * FROM character, character_actor WHERE character.id NOT LIKE character_a
现在这个程序成功地反转了键盘输入的单词。但是我想在我反转它之前“保存”指针中的单词,所以我可以比较两者,反转的和“原始的”,并检查它们是否是回文。我还没有太多经验,可能会出现比我知道的更多的错误,但我
Memcpy 和 memcmp 函数可以接受指针变量吗? char *p; char* q; memcpy(p,q,10); //will this work? memcmp(p,q,10); //w
恐怕我对一个相当过饱和的主题的细节有疑问,我搜索了很多,但找不到一个明确的答案来解决这个特定的明显-imho-重要的问题: 使用UTF-8将byte[]转换为String时,每个字节(8bit)都变成
我有一个奇怪的问题。我需要从 stat 命令打印输出字符串。 我已经编写了获取一些信息的代码。 import glob import os for file in glob.glob('system1
我正在使用 Java 并具有其值如下所示的字符串, String data = "vale-cx"; data = data.replaceAll("\\-", "\\-\\"); 我正在替换其中的“
String urlParameters = "login=test&password=te&ff"; 我有一个String urlParams,& - 是密码的一部分,如何使其转义,从而不被识别为分
大家好,我只想从此字符串中提取第一个字母: String str = "使 徒 行 傳 16:31 ERV-ZH"; 我只想获取这些字符: 使 徒 行 傳 并且不包括 ERV-ZH 仅数
这个问题已经有答案了: Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized point
所以, 我有一个字符**;它本质上是一个句子,带有指向该句子中每个单词的指针;即 'h''i''\0''w''o''r''l''d''\0''y''a''y''!''\0' 在这种情况下,我希望使用可
这个问题在这里已经有了答案: Using quotation marks inside quotation marks (12 个答案) 关闭 7 年前。 如何打印 " 字符? 我知道打印 % 符号
我是一名优秀的程序员,十分优秀!