网络_HttpURLConnection_原始类

// get 请求,取得响应
public class GetRunnable implements Runnable {
	private Handler myhand;
	public MyRunnable(Handler hand) {
		this.myhand = hand;
	}
	@Override
	public void run() {
		// http://192.168.1.29:8080/itheima83/servlet/LoginServlet?username=sdafsad&pwd=fsadf
		String message = "";
		try {
			URL url = new URL("http://192.168.1.29:8080/itheima83/servlet/LoginServlet?username=sdafsad&pwd=fsadf");
			HttpURLConnection http = (HttpURLConnection) url.openConnection();
			http.setConnectTimeout(10000);
			http.setRequestMethod("GET");
			int status = http.getResponseCode();
			if (status < 300) {
				InputStream is = http.getInputStream();
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024 * 10];
				int len = 0;
				while ((len = is.read(buffer)) != -1) {
					bos.write(buffer, 0, len);
				}
				message = bos.toString();
				bos.close();
			}
			Message msg = new Message();
			msg.obj = message;
			myhand.sendMessage(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



// post 请求,取得响应
URL url = new URL("http://192.168.1.29:8080/itheima83/servlet/LoginServlet");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setConnectTimeout(10000);
http.setRequestMethod("POST");
http.setDoOutput(true);
//下面两行,不加同样可以被服务器解析。最好加上。
http.setRequestProperty("Content-type", "application/x-www-form-urlencoded");//  multipart/form-data
http.setRequestProperty("Content-Length", content.length()+"");


http.getOutputStream().write("username=sdafsad&pwd=fsadf".getBytes());


int status = http.getResponseCode();


if (status < 300) {
		//获得输入流,进行读取使用
}






// post 提交文件
一般不用这个,因为post提交的标准请求体格式不好包装。尤其是 multipart类型的包装。
这个包装包括请求头的 content的长度,编码等内容。。
当然,如果自己写服务端,就无所谓了。




//下载。这个没什么总结的。通过无论什么请求,服务器把响应返回。
//响应包含响应头,里面有文件大小等信息




//单线程下载,多线程下载
//多线程的 Range 请求。和 content-Length的平均startIndex,endIndex。
// 断点续传,记录下载的位置点
class MyRunnable implements Runnable{
	private String url_st;
	private String file_path;
	private int start;
	private int end;
	public MyRunnable(String url_st, String file_path, int start, int end) {
		this.url_st = url_st;
		this.file_path = file_path;
		this.start = start;
		this.end = end;
		System.out.println(">>>>>>>>>开启一个新的线程。start:"+start+" end:"+end);
	}
	@Override
	public void run() {


		if(cacheFile.exsit()){
			FileInputStream fileInputStream = new FileInputStream(file3);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
			lastSaveIndex = Integer.parseInt(bufferedReader.readLine(););
		}


		try {
			URL url = new URL(url_st);
			HttpURLConnection http = (HttpURLConnection) url.openConnection();
			http.setRequestMethod("GET");
			http.setRequestProperty("Range", "bytes="+start+"-"+end);
			http.setConnectTimeout(10000);
			//开始写
			InputStream is = http.getInputStream();
			RandomAccessFile raf = new RandomAccessFile(file_path,"rwd");
			raf.seek(start);
			byte[] buffer = new byte[1024*10];
			int len =0;
			while((len = is.read(buffer)) != -1){
				raf.write(buffer, 0, len);


				//标记写的位置,可以用来续传
				File file2 = new File( getdownloadPath()+threadId+".txt");
				RandomAccessFile randomAccessFile2 = new RandomAccessFile(file2, "rwd");
				randomAccessFile2.write(String.valueOf(currentThreadDownloadPosition).getBytes());
				randomAccessFile2.close();
				//利用 progressbard 的引用。改变 progressbar 的进度。
				progressBar2.setProgress(progress);
			}
			raf.close();
			http.disconnect();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值