Http 가 네트워크 에 접근 하 는 GET 와 POST

Http访问网络之GET和POST_第1张图片
Http访问网络之GET和POST_第2张图片
Http访问网络之GET和POST_第3张图片
Http访问网络之GET和POST_第4张图片
Http访问网络之GET和POST_第5张图片
public class HttpGetUtil {

	//  get       
	public static InputStream getInputStream() {
		InputStream inputStream = null;
		HttpURLConnection httpURLConnection = null;
		try {
			URL url = new URL("http://www.baidu.com/img/bd_logo1.png");
			if (url != null) {
				httpURLConnection = (HttpURLConnection) url.openConnection();
				//            
				httpURLConnection.setConnectTimeout(3000);
				//      
				httpURLConnection.setDoInput(true);
				//   GET    
				httpURLConnection.setRequestMethod("GET");
				//        
				int responseCode = httpURLConnection.getResponseCode();
				if (responseCode == 200) {
					inputStream = httpURLConnection.getInputStream();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return inputStream;
	}

	//         
	public void saveImageToDisk() {
		InputStream inputStream = getInputStream();
		byte[] data = new byte[1024];
		int len = 0;
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream("D://test.png");
			while ((len = inputStream.read(data)) != -1) {
				fileOutputStream.write(data, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
public class HttpPostUtil {
	private static URL url;

	//  post       
	public static String sendPostMessage(Map<String, String> params, String encode) {
		//    
		StringBuffer buffer = new StringBuffer();
		buffer.append("?");
		try {
			if (params != null && !params.isEmpty()) {
				for (Map.Entry<String, String> entry : params.entrySet()) {
					//     
					buffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), encode))
							.append("&");
				}
				//       &
				buffer.deleteCharAt(buffer.length() - 1);
			}

			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setConnectTimeout(3000);
			urlConnection.setRequestMethod("POST");
			urlConnection.setDoInput(true);//         
			urlConnection.setDoOutput(true);//        
			//                
			byte[] mydata = buffer.toString().getBytes();
			//                
			urlConnection.setRequestProperty("Content-type", "application/x-www-from-urlencoded");
			urlConnection.setRequestProperty("Content-length", String.valueOf(mydata.length));
			//      ,        
			OutputStream outputStream = urlConnection.getOutputStream();
			outputStream.write(mydata, 0, mydata.length);
			outputStream.close();
			int responseCode = urlConnection.getResponseCode();
			if (responseCode == 200) {
				return changeInputStream(urlConnection.getInputStream(), encode);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	//             
	private static String changeInputStream(InputStream inputStream, String encode) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		int len = 0;
		String result = "";
		if (inputStream != null) {
			try {
				while ((len = inputStream.read()) != -1) {
					outputStream.write(data, 0, len);
				}
				result = new String(outputStream.toByteArray(), encode);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

}

좋은 웹페이지 즐겨찾기