首页新闻找找看学习计划

新浪微博API调用问题

0
悬赏园豆:50 [已解决问题] 解决于 2012-08-20 10:27

昨天调用还OK,今天一大早过来就调用不了  

抛出SSL异常,Connection reset by peer

急求问题所在

问题补充:
    private void getWeiboResult(String url) {
        HttpClient httpClient = getNewHttpClient();
        HttpGet httpGet = new HttpGet(url);
//        HttpPost httpPost=new HttpPost(url);
        InputStream inputStream = null;
        HttpResponse response;
        HttpEntity entity;

        try {
            response = httpClient.execute(httpGet);
            entity = response.getEntity();
            inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String result = "";
            String line = "";

            while ((line = reader.readLine()) != null) {
                result = result + line;
            }
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public static HttpClient getNewHttpClient() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);

            SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));

            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }

SSLSocketFactoryEx.class:
public class SSLSocketFactoryEx extends SSLSocketFactory {

    private SSLContext sslContext = SSLContext.getInstance("TLS");

    public SSLSocketFactoryEx(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException,
            UnrecoverableKeyException {
        super(truststore);

        TrustManager tm = new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                    throws java.security.cert.CertificateException {

            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
                    throws java.security.cert.CertificateException {

            }
        };

        sslContext.init(null, new TrustManager[] { tm }, null);
    }

    @Override
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
        return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
    }

    @Override
    public Socket createSocket() throws IOException {
        return sslContext.getSocketFactory().createSocket();
    }

}
暗殇的主页 暗殇 | 初学一级 | 园豆:66
提问于:2012-07-19 15:23
< >
分享
最佳答案
0

很有可能是新浪服务器的问题,新浪的服务一直都不太稳定,你可以换时间多试几次

收获园豆:50
artwl | 专家六级 |园豆:16526 | 2012-07-19 15:40

不应该是新浪那边的问题

我在浏览器直接拷贝那个地址可以访问到数据 

写个Demo 发个别人 同样一段代码 别人能拿到 我这里拿不到

我就挺奇怪的

而且同样一个APK 别人能拿到 我这里拿不到

我的app还未通过审核!!!

暗殇 | 园豆:66 (初学一级) | 2012-07-19 15:43

@暗殇: 调用太频繁也有可能产生这个错误的,另外新浪微博对调用频率有限制的。

artwl | 园豆:16526 (专家六级) | 2012-07-19 15:53

@artwl: 我知道新浪微博用调用频率这个问题 

但是未授权的应用 一个小时也可以访问50多次 

何况我访问的频率根本不会那么高

暗殇 | 园豆:66 (初学一级) | 2012-07-19 16:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册