码迷,mamicode.com
首页 > 编程语言 > 详细

java 网络请求

时间:2015-08-05 12:52:13      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:java

String ret = null;
		try {

			URL url = new URL(requrl);
			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
			httpConn.setConnectTimeout(30000);
			httpConn.setReadTimeout(30000);
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			httpConn.setRequestMethod("POST");
			DataOutputStream out = new DataOutputStream(httpConn.getOutputStream());
			out.write(paras.getBytes("UTF-8"));
			out.flush();
			out.close();
			InputStream stream = httpConn.getInputStream();

			DataInputStream in = new DataInputStream(stream);
			byte[] bin = null;
			byte[] inc = new byte[1024];
			int datelength = 0;
			int insize = 0;
			while ((insize = in.read(inc)) != -1) {
				int oldlength = datelength;
				datelength += insize;
				byte[] oldbin = new byte[datelength];
				for (int i = 0; i < oldlength; i++)
					oldbin[i] = bin[i];
				for (int i = oldlength; i < datelength; i++)
					oldbin[i] = inc[i - oldlength];
				bin = oldbin;
			}
			ret = new String(bin, "UTF8");
			in.close();
			System.out.println("ret is: " + ret);
		} catch (Exception ex) {
			ex.printStackTrace();

		}
		return ret;

版权声明:本文为博主原创文章,未经博主允许不得转载。

java 网络请求

标签:java

原文地址:http://blog.csdn.net/daixinmei/article/details/47294749

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!