`
xiaofan_0204
  • 浏览: 124994 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java 中的HTTP操作

    博客分类:
  • Java
阅读更多
URL url = new URL("http://www.sdfsdfasd.com");
		HttpURLConnection  httpURLConnection  = (HttpURLConnection)url.openConnection();
		httpURLConnection.setRequestMethod("GET");
		httpURLConnection.setConnectTimeout(30000);
		try {
			httpURLConnection.connect();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		System.out.println("----");
		int responseCode = httpURLConnection.getResponseCode();
		System.out.println("responseCode:" + responseCode);
		InputStream inputStream = httpURLConnection.getInputStream();
		
		byte[] bytes = new byte[1024];
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		
		int length = 0;
		while((length = inputStream.read(bytes)) != -1){
			byteArrayOutputStream.write(bytes,0,length);
		}
		byte[] byteSum = byteArrayOutputStream.toByteArray();
		
		String urlContent = new String(byteSum);
		byteArrayOutputStream.close();
		inputStream.close();
		System.out.println(urlContent);

详细信息:http://www.cnblogs.com/guodongli/archive/2011/04/05/2005930.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics