资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java操作newHttpPost(URL)和newURL(shixun_ip)获取流的方法

java操作new HttpPost(URL)和 new URL(shixun_ip) 获取流的方法?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!

创新互联公司主营洪雅网站建设的网络公司,主营网站建设方案,app软件定制开发,洪雅h5微信小程序搭建,洪雅网站营销推广欢迎洪雅等地区企业咨询

HttpClient:

private static HttpClient httpClient;
	static {
		if (httpClient == null) {
			HttpParams params = new BasicHttpParams();
			// 设置一些基本参数
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			// 超时设置 /* 从连接池中取连接的超时时间 */
			ConnManagerParams.setTimeout(params, 1000);
			/* 连接超时 */
			HttpConnectionParams.setConnectionTimeout(params, 2000);
			/* 请求超时 */
			HttpConnectionParams.setSoTimeout(params, 4000);
			SchemeRegistry schReg = new SchemeRegistry();
			schReg.register(new Scheme("http", PlainSocketFactory
					.getSocketFactory(), 80));
			schReg.register(new Scheme("https", SSLSocketFactory
					.getSocketFactory(), 443));
			// 使用线程安全的连接管理来创建HttpClient
			ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
					params, schReg);
			httpClient = new DefaultHttpClient(conMgr, params);
		}
	}
@SuppressWarnings("unchecked")
	private  T getJsonObjectFromUrl(String urlStr,
			List params, Class className) throws Exception{
		T object = null;
 
		InputStream in = null;
		try {
			HttpPost httpPost = new HttpPost(urlStr);
 
			if (params != null) {
				//				List nvps = new ArrayList();
				//				for (Entry entry : params.entrySet()) {
				//					nvps.add(new BasicNameValuePair(entry.getKey(), entry
				//							.getValue()));
				//				}
				httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			}
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
 
			if(entity!=null){
				in= entity.getContent(); //之前没使用这个造成了大量异常抛出
			}
 
			/*String s = EntityUtils.toString(in, "UTF-8");
			if (entity != null) {
				EntityUtils.consume(entity);
			}*/
 
			String s = inputStream2String(in,"UTF-8");
			JsonResult jsonResult = (JsonResult)JSONObject.parseObject(s, JsonResult.class);
			if(jsonResult.getCode() == 200){
				Object obj = jsonResult.getData();
 
				if(obj != null){
					if(className.isInstance(obj)){
						object = (T)(obj);
					}
					else if(obj instanceof JSONObject){
						object= JSONObject.parseObject(obj.toString(), className);
					}					
				}
			}else{
				throw new Exception(jsonResult.getMsg());
			}
 
			if(in!=null){
				in.close();
			}
 
		} catch (Exception e) {
			throw e;
		}
 
		return object;
	}

new URL():

try{
					
					//省略
					URL shixun = new URL(url);
					InputStream in = shixun.openStream();
 
					
					long end = System.currentTimeMillis();  
					if((end-start)<=5000){  //5s内不响应就跳过
						
						String ret = cn.com.jsoft.jframe.utils.StringUtils.toString(in, "utf-8");
						JSONObject obj = JSONObject.fromObject(ret);
						if(obj!=null&&obj.getJSONObject("info")!=null&&obj.containsKey("info")==true){
							JSONObject info = obj.getJSONObject("info");
							JSONArray data = info.getJSONArray("data");
							if(data!=null&&data.size()>0){
								for(int i=0;i

java将InputStream转为String

public static String inputStream2String(InputStream is, String charset) {
		ByteArrayOutputStream baos = null;
 
		try {
			baos = new ByteArrayOutputStream();
			int i = -1;
			while ((i = is.read()) != -1) {
				baos.write(i);
			}
			return baos.toString(charset);
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("FileWRUtil.inputStream2String(InputStream is, String charset) occur error:"
					+ e.getMessage());
		} finally {
			if (null != baos) {
				try {
					baos.close();
				} catch (IOException e) {
					e.printStackTrace();
					logger.error("FileWRUtil.inputStream2String(InputStream is, String charset) occur error:"
							+ e.getMessage());
				}
				baos = null;
			}
		}
 
		return null;
	}

感谢各位的阅读!看完上述内容,你们对java操作new HttpPost(URL)和 new URL(shixun_ip) 获取流的方法大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注创新互联行业资讯频道。


当前标题:java操作newHttpPost(URL)和newURL(shixun_ip)获取流的方法
转载来源:http://cdkjz.cn/article/jpgohg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220