java解析url 得到返回值

public String getReturnData(String urlString) throws UnsupportedEncodingException {  

     String res = "";   

        try {   

            URL url = new URL(urlString);  

            java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();  

            conn.setDoOutput(true);  

            conn.setRequestMethod("POST");  

            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));  

            String line;  

           while ((line = in.readLine()) != null) {  

               res += line+"\n";  

         }  

            in.close();  

        } catch (Exception e) {  

            logger.error("error in wapaction,and e is " + e.getMessage());  

        }  

//      System.out.println(res);   

        return res;  

    }  

 

 

方法二:

package com.gamemag.web.statistics;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import net.sf.json.JSONObject;

public class DataInterface {

	HostnameVerifier hv = new HostnameVerifier() {
		public boolean verify(String urlHostName, SSLSession session) {
			System.out.println("Warning: URL Host: " + urlHostName + " vs. "
					+ session.getPeerHost());
			return true;
		}
	};

	/**
	 * Retrieves the response from the server by opening a connection and merely
	 * reading the response.
	 * 
	 * @throws MalformedURLException
	 */
	protected final int getReturnData(final String urlString,
			final String ticket) throws MalformedURLException {
		URL url = new URL(urlString);
		HttpURLConnection connection = null;
		int name = 0;
		try {
			trustAllHttpsCertificates();
			HttpsURLConnection.setDefaultHostnameVerifier(hv);

			connection = (HttpURLConnection) url.openConnection();
			final BufferedReader in = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));

			String line;
			final StringBuffer stringBuffer = new StringBuffer(255);

			synchronized (stringBuffer) {
				while ((line = in.readLine()) != null) {
					stringBuffer.append(line);
					stringBuffer.append("\n");
				}
				//return stringBuffer.toString();
			}
			Object obj = stringBuffer.toString();
			JSONObject jsonObject = JSONObject.fromObject(obj);
			name = jsonObject.getInt("rst");

		} catch (final IOException e) {
			e.printStackTrace();
		} catch (final Exception e1) {
			e1.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		return name;
	}

	private static void trustAllHttpsCertificates() throws Exception {
		javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
		javax.net.ssl.TrustManager tm = new miTM();
		trustAllCerts[0] = tm;
		javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
				.getInstance("SSL");
		sc.init(null, trustAllCerts, null);
		javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
				.getSocketFactory());
	}

	static class miTM implements javax.net.ssl.TrustManager,
			javax.net.ssl.X509TrustManager {
		public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public boolean isServerTrusted(
				java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public boolean isClientTrusted(
				java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public void checkServerTrusted(
				java.security.cert.X509Certificate[] certs, String authType)
				throws java.security.cert.CertificateException {
			return;
		}

		public void checkClientTrusted( 
				java.security.cert.X509Certificate[] certs, String authType)
				throws java.security.cert.CertificateException {
			return;
		}
	}

}


调用方法:
DataInterface dateInter = new DataInterface();
int userNum = dateInter.getReturnData("https://"+ip+":5000/rpc/lib_http_rpc:getusers","1");

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值