Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么通过HttpURLConnection getInputStream的内容与通过Linux命令curl或wget的内容不同?_Java_Xml_Curl_Wget - Fatal编程技术网

Java 为什么通过HttpURLConnection getInputStream的内容与通过Linux命令curl或wget的内容不同?

Java 为什么通过HttpURLConnection getInputStream的内容与通过Linux命令curl或wget的内容不同?,java,xml,curl,wget,Java,Xml,Curl,Wget,守则: private static final String HTTP_URL = "http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest"; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub java.net.URL url = n

守则:

private static final String HTTP_URL =
        "http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest";
/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    java.net.URL url = null;

    try {
        url = new java.net.URL(HTTP_URL);
        URLConnection conn = null;
        conn = url.openConnection();
        conn.setConnectTimeout(15000);
        conn.setReadTimeout(10000);
        conn.setDoInput(true);
        conn.connect();
        InputStream in = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(in);

        FileOutputStream fos = new FileOutputStream(new File("tmp.xml"));

        byte[] buffer = new byte[2*1024];
        int bytesRead;
        while ((bytesRead = bis.read(buffer)) > -1) {
            fos.write(buffer, 0, bytesRead);
        }
        fos.flush();
        fos.close();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
获取内容:

<?xml version="1.0" encoding="utf-8"?>

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:re="http://purl.org/atompub/rank/1.0">
    <title type="text">Newest questions tagged android - Stack Overflow</title>
    <link rel="self" href="http://stackoverflow.com/feeds/tag?tagnames=android&amp;sort=newest" type="application/atom+xml" />
    <link rel="alternate" href="http://stackoverflow.com/questions/tagged/?tagnames=android&amp;sort=newest" type="text/html" />
    <subtitle>most recent 30 from stackoverflow.com</subtitle>
    <updated>2014-11-06T03:40:16Z</updated>
    <id>http://stackoverflow.com/feeds/tag?tagnames=android&amp;sort=newest</id>
    <creativeCommons:license>http://www.creativecommons.org/licenses/by-sa/3.0/rdf</creativeCommons:license>
    <entry>
        <id>http://stackoverflow.com/q/26771304</id>
        <re:rank scheme="http://stackoverflow.com">0</re:rank>
        <title type="text">What is the correct data/operation flow of the new Camera2 api?</title>
            <category scheme="http://stackoverflow.com/tags" term="android" />
            <category scheme="http://stackoverflow.com/tags" term="multithreading" />
            <category scheme="http://stackoverflow.com/tags" term="opengl" />
            <category scheme="http://stackoverflow.com/tags" term="android-camera" />
            <category scheme="http://stackoverflow.com/tags" term="android-5.0-lollipop" />
        <author>
            <name>user977289</name>
            <uri>http://stackoverflow.com/users/977289</uri>
        </author>
        <link rel="alternate" href="http://stackoverflow.com/questions/26771304/what-is-the-correct-data-operation-flow-of-the-new-camera2-api" />
        <published>2014-11-06T03:36:53Z</published>
        <updated>2014-11-06T03:36:53Z</updated>
        <summary type="html">

最新问题-堆栈溢出
stackoverflow.com最近发布的30条
2014-11-06T03:40:16Z
http://stackoverflow.com/feeds/tag?tagnames=android&排序=最新
http://www.creativecommons.org/licenses/by-sa/3.0/rdf
http://stackoverflow.com/q/26771304
0
新Camera2 api的正确数据/操作流程是什么?
用户977289
http://stackoverflow.com/users/977289
2014-11-06T03:36:53Z
2014-11-06T03:36:53Z
使用wget获取的内容:

<!DOCTYPE html>
<html itemscope itemtype="http://schema.org/QAPage">
<head>
    <title>Newest &#39;android&#39; Questions - Stack Overflow</title>
    <link rel="shortcut icon" href="//cdn.sstatic.net/stackoverflow/img/favicon.ico?v=038622610830">
    <link rel="apple-touch-icon image_src" href="//cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png?v=fd7230a85918">
    <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:domain" content="stackoverflow.com"/>
    <meta property="og:type" content="website" />
    <meta property="og:image" itemprop="image primaryImageOfPage" content="http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png?v=fde65a5a78c6" />
    <meta name="twitter:title" property="og:title" itemprop="title name" content="Newest &amp;#39;android&amp;#39; Questions" />
    <meta name="twitter:description" property="og:description" itemprop="description" content="Q&amp;A for professional and enthusiast programmers" />
    <meta property="og:url" content="http://stackoverflow.com/questions/tagged/?tagnames=android&amp;sort=newest"/>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="//cdn.sstatic.net/Js/stub.en.js?v=7bd5359278db"></script>
    <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/stackoverflow/all.css?v=847253e69e95">

    <link rel="alternate" type="application/atom+xml" title="newest android questions feed" href="/feeds/tag?tagnames=android&amp;sort=newest" />

    <script>
        StackExchange.init({"locale":"en","stackAuthUrl":"https://stackauth.com","serverTime":1415242159,"networkMetaHostname":"meta.stackexchange.com","routeName":"Questions/ListByTag","styleCode":true,"enableUserHovercards":true,"snippets":{"enabled":true,"domain":"stacksnippets.net"},"site":{"name":"Stack Overflow","description":"Q&A for professional and enthusiast programmers","isNoticesTabEnabled":true,"recaptchaPublicKey":"6LdchgIAAAAAAJwGpIzRQSOFaO0pU6s44Xt8aTwc","recaptchaAudioLang":"en","enableNewTagCreationWarning":true,"nonAsciiTags":true,"enableSocialMediaInSharePopup":true},"user":{"fkey":"43e8d647b5e1e33123144dc39f3efa41","isAnonymous":true,"ab":{"tutorial_tooltips":{"v":"b","g":1},"homepage_recommended_algo":{"v":"c","g":1}}}});
        StackExchange.using.setCacheBreakers({"js/prettify-full.en.js":"73a6d05be8f7","js/moderator.en.js":"3bf88d4bfb80","js/full-anon.en.js":"f36061371874","js/full.en.js":"91631f8e1ecf","js/wmd.en.js":"bd137db5e76f","js/third-party/jquery.autocomplete.min.js":"e5f01e97f7c3","js/third-party/jquery.autocomplete.min.en.js":"","js/mobile.en.js":"6f90cb09d971","js/help.en.js":"56d0a3a560ae","js/tageditor.en.js":"fb6ca50266c2","js/tageditornew.en.js":"f6e3914262e8","js/inline-tag-editing.en.js":"1a5cabd8e30a","js/revisions.en.js":"989c474e85a9","js/review.en.js":"cce796af5ac7","js/tagsuggestions.en.js":"bb4721d888d2","js/post-validation.en.js":"ecf3ea435618","js/explore-qlist.en.js":"4e6f969d97ee","js/events.en.js":"e3f42c90a83b","js/keyboard-shortcuts.en.js":"feb9597a8451","js/external-editor.en.js":"ea8052d19ea8","js/external-editor.en.js":"ea8052d19ea8","js/snippet-javascript.en.js":"f32a62fbdc0b","js/snippet-javascript-codemirror.en.js":"7e172e0797e2"});
        StackExchange.using("gps", function() {
             StackExchange.gps.init(true);
        });
    </script>

    <script>
        StackExchange.ready(function () {
            $('#nav-tour').click(function () {
                StackExchange.using("gps", function() {
                    StackExchange.gps.track("aboutpage.click", { aboutclick_location: "headermain" }, true);
                });
            });
        });
    </script>
</head>

<body class="tagged-questions-page new-topbar" lang="">
    <noscript><div id="noscript-padding"></div></noscript>
    <div id="notify-container"></div>
    <div id="overlay-header"></div>
    <div id="custom-header"></div>

最新&39;安卓&39;问题-堆栈溢出
init({“locale”:“en”,“stackAuthUrl”:”https://stackauth.com“,”serverTime“:1415242159,“networkMetaHostname:”meta.stackexchange.com“,”routeName:”Questions/ListByTag“,”styleCode“:”true“,”enableUserHovercards“:”true“,”snippets“:{”enabled“:”true,“domain“:”stacksnippets.net”},“site:“{”name:“Stack Overflow”,“description”:“专业和热心程序员的问答”,“IsNotifiestabenable”:true,“RecaptChaPublicey”:“6ldchgiaaaaaAjWgpiZrqsof0pu6s44xt8atwc”,“recaptchaAudioLang”:“en”,“enableNewTagCreationWarning”:true,“nonAsciiTags”:true,“enableSocialMediaSharePopup”:true},“user”:{“fkey”:“43e8d647b5e1e3323144dc39f341”,“isAnonymous”:true,“ab”:{”教程工具提示:{“v”:“b”,“g”:1},“主页”{“v”:“c”,“g”:1}});
StackExchange.using.SetCacheBrukers({“js/prettify full.en.js”:“73a6d05be8f7”、“js/mediator.en.js”:“3BF88D4BB80”、“js/full-anon.en.js”:“f36061371874”、“js/full.en.js”:“91631f8e1ecf”、“js/wmd.en.js”:“bd137db5e76f”、“js/third/jquery.autocomplete.min.js”:“e5f01e97f7c3”、“js/third/jquery.autocomplete.js”:“移动设备”6f90cb09d971,“js/help.en.js”:“56d0a3a560ae”,“js/tageditor.en.js”:“fb6ca50266c2”,“js/tageditornew.en.js”:“f6e3914262e8”,“js/inline tag editing.en.js”:“1a5cabd8e30a”,“js/revisions.en”:“989c474e85a9”,“js/review.js”:“cce796af5ac7”,“js/tagadvisions.en.js”:“bb4721d888d2”,“js/post validation.en.js”:“ECF3EA618”,“QLEN.js”:”:”4e6f969d97ee,“js/events.en.js”:“e3f42c90a83b”,“js/keyboard shortcuts.en.js”:“feb9597a8451”,“js/external editor.en.js”:“ea8052d19ea8”,“js/snippet javascript.en.js”:“F32AFB62DC0B”,“js/snippet javascript codemirr.en.js”:“7e172e0797e2”);
StackExchange.using(“gps”,function()){
StackExchange.gps.init(true);
});
StackExchange.ready(函数(){
$(“#导航漫游”)。单击(函数(){
StackExchange.using(“gps”,function()){
StackExchange.gps.track(“aboutpage.click”{aboutclick_location:“headermain”},true);
});
});
});

它们完全不同,为什么?如何使用任何工具获取XML格式的内容?

唯一可能导致stackoverflow服务器以不同结果响应的是您用Java或wget发送的HTTP头(例如,Accept或User Agent)。因此,您必须在这两种情况下检查HTTP标头。为了在wget中查看它们,您可以使用以下参数运行它:

wget -d http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest
-d
参数强制
wget
命令打印请求和响应HTTP头,以便您可以将它们与Java版本中相应的值进行比较