java.net.protocolException with HttpsURLConnection in android

亡梦爱人 提交于 2019-12-11 13:52:58

问题


I am trying to create a FlickR request through HttpURL connection and I get a java.net.protocolException on con.getInputStream()

        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    URL turl = new URL(url);                        
                    HttpsURLConnection con = (HttpsURLConnection)turl.openConnection();                     
                    InputStream inputStream = con.getInputStream();
                    xml = getStringFromInputStream(inputStream);
                    uihandler.post(obj.updateRunnable);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

the url is

static final String URL = "https://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=XXXXXXXXXX";

Exception Log

07-07 23:47:36.980: W/System.err(1923): java.net.ProtocolException: Unexpected status line: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"    "http://www.w3.org/TR/html4/strict.dtd"> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ERROR: Cache Access Denied</title> <style type="text/css"><!--   /* Page basics */
07-07 23:47:37.020: D/dalvikvm(1923): GC_FOR_ALLOC freed 204K, 9% free 3077K/3356K, paused 32ms, total 35ms
07-07 23:47:37.020: W/System.err(1923):     at  com.android.okhttp.internal.http.RawHeaders.setStatusLine(RawHeaders.java:108)
07-07 23:47:37.020: W/System.err(1923):     at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:308)
07-07 23:47:37.030: W/System.err(1923):     at com.android.okhttp.Connection.makeTunnel(Connection.java:311)
07-07 23:47:37.030: W/System.err(1923):     at com.android.okhttp.Connection.upgradeToTls(Connection.java:127)
07-07 23:47:37.030: W/System.err(1923):     at com.android.okhttp.Connection.connect(Connection.java:107)
07-07 23:47:37.030: W/System.err(1923):     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
07-07 23:47:37.030: W/System.err(1923):     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
07-07 23:47:37.030: W/System.err(1923):     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
07-07 23:47:37.030: W/System.err(1923):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
07-07 23:47:37.030: W/System.err(1923):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
07-07 23:47:37.030: W/System.err(1923):     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:161)
07-07 23:47:37.030: W/System.err(1923):     at com.example.flickr.XMLParser$2.run(XMLParser.java:99)
07-07 23:47:37.030: W/System.err(1923):     at java.lang.Thread.run(Thread.java:841)

any help would be appreciated.


回答1:


    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"    "http://www.w3.org/TR/html4/strict.dtd"> <html><head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ERROR: Cache Access Denied</title> <style type="text/css"><!--   /* Page basics */

You are getting html instead of xml, the reason you are getting an exception.
Check your Url.




回答2:


The server is kindly failing to return a correctly formed HTTP response. Instead it is just returning straight HTML, no HTTP headers. Nothing you can do about it at the client end. Complain to the server people. If that's you, fix it.

BTW Nothing to do with HTTPS or SSL, which is working perfectly,



来源:https://stackoverflow.com/questions/24623432/java-net-protocolexception-with-httpsurlconnection-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!