Facebook Graph API SSL套接字握手异常

时间:2015-05-13 12:02:19

标签: java facebook sockets facebook-graph-api ssl

这是一个示例程序,iam使用图形api测试facebook数据集合。在我的整个项目中,我无法找到错误的根本原因。第一次从程序中收集数据。但第二次显示ssl异常。

public class Testing {

    public static void main(String[] args) throws IOException {

        // Make a URL to the web page
        boolean flag=false;
        int count=0;
        while(true)
        {

            count++;

            if(count==2)
            {

                flag=false;
            }

        System.out.println(new Date());
        URL url = new URL("https://graph.facebook.com/fql?q=select+message,post_id,created_time,actor_id,attachment,comments,created_time,impressions,likes,message_tags,place,share_count+from+stream+where+source_id+in(select+page_id+from+page+where+username=%27ATT%27)+and+created_time>1368439594&access_token=[HERE STANDS MY ACCESSTOKEN]");
        System.out.println(new Date());
        // Get the input stream through URL Connection
        URLConnection con = url.openConnection();
        InputStream is =con.getInputStream();

        // Once you have the Input Stream, it's just plain old Java IO stuff.

        // For this case, since you are interested in getting plain-text web page
        // I'll use a reader and output the text content to System.out.

        // For binary content, it's better to directly read the bytes from stream and write
        // to the target file.


        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        String line = null;

        // read each line and write to System.out
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

        }
    }
}

请指导我解决这个问题。

错误:

线程中的异常" main" javax.net.ssl.SSLHandshakeException:握手期间远程主机关闭连接     at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)     at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)     在sun.security.ssl.SSLSocketImpl.startHandshake(未知来源)     在sun.security.ssl.SSLSocketImpl.startHandshake(未知来源)     在sun.net.www.protocol.https.HttpsClient.afterConnect(未知来源)     at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)     at span.com.Testing.main(Testing.java:28) 引起:java.io.EOFException:SSL对等关闭不正确     at sun.security.ssl.InputRecord.read(Unknown Source)     ... 9更多

1 个答案:

答案 0 :(得分:0)

您的网址是HTTPS网址,这意味着它使用SSL,但您尝试使用URLConnection打开连接。要打开HTTPS网址,您应该使用HttpsURLConnection

进行操作
final HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

有了这个改变,它对我有用。 ;)