java原始包net中,有支持http的get和post的请求类。直接看代码
get请求:
public class httpTest { public static void main(String[] args) { new Runs().start(); } static class Runs extends Thread{ @Override public void run() { try { URL url = new URL("http://www.9k9kh5.com:8080/BigMillionaire/H52/index.html"); //创建一个网络路径对象 URLConnection connection = url.openConnection();//打开网络路径对象中的链接。将返回一个URLConnection对象。 InputStream is = connection.getInputStream();//获取返回对象中的输入流 InputStreamReader isr = new InputStreamReader(is); //把流包装到字符输入流 BufferedReader br = new BufferedReader(isr);//包装到buff中 String line; StringBuilder builder = new StringBuilder(); while((line = br.readLine())!=null){ builder.append(line); } br.close(); isr.close(); is.close(); System.out.println(builder.toString()); } catch (MalformedURLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } }
总结:向指定路径发送了请求,并将结果封装到URLConnection对象中,通过流将内容读出。
post请求;
public class HttpPost { public static void main(String[] args) { new RunPost().start(); } } class RunPost extends Thread{ @Override public void run() { try { URL url = new URL("http://www.9k9kh5.com:8080/BigMillionaire/H52/index.html"); HttpURLConnection post = (HttpURLConnection) url.openConnection();//打开链接,并返回一个post对象 post.addRequestProperty("encoding", "utf-8"); post.setRequestMethod("POST"); //设置链接网络的方式为post post.setDoInput(true);//可以从网络获取到数据。 post.setDoOutput(true);//可以传输数据到网络 //输出流 OutputStream os = post.getOutputStream(); OutputStreamWriter sw = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(sw); bw.write("userId=27&sign=965435ff254fbcffee25c0d2b7e92dc8"); bw.flush();//刷新(强制输出) //输入流(输入流必须要先向网络发送数据结束后才可用) InputStream is = post.getInputStream(); InputStreamReader rs = new InputStreamReader(is); BufferedReader br = new BufferedReader(rs); String line; StringBuilder builder =new StringBuilder(); while((line=br.readLine())!=null){ builder.append(line); } System.out.println(builder.toString()); bw.close(); sw.close(); os.close(); br.close(); rs.close(); is.close(); } catch (MalformedURLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }
总结:和get不同的是请求只有路径,参数是通过输出流传输的。通过post请求返回的对象是HttpURLConnection。其他和get相似。
利用第三方jar包。
第三方工具(apache的httpClient)
官网:hc.apache.org
httpcomponents-client-4.5-bin.ziphttpcomponents-client-4.5lib (包名)
public class HttpClient { public static void main(String[] args) { new GetandPost ().start(); } } class GetandPost extends Thread{ CloseableHttpClient client = HttpClients.createDefault(); //创建出来对象 @Override public void run() { HttpPost post = new HttpPost("http://123.59.33.252:9999/tvData");//创建post请求对象 try { List<BasicNameValuePair> list= new ArrayList<BasicNameValuePair>(); //创建参数集合 list.add(new BasicNameValuePair("TVname","27")); //添加参数 post.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));//把参数放入请求对象,,post发送的参数list,指定格式 CloseableHttpResponse response = client.execute(post);//启动执行请求,并获得返回值 HttpEntity entity = response.getEntity();//得到返回的entity对象 String result = EntityUtils.toString(entity, "UTF-8");//把实体对象转换为string System.out.println(result); } catch (UnsupportedEncodingException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } catch (ClientProtocolException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } // HttpGet get = new HttpGet("http://www.9k9kh5.com:8080/BigMillionaire/H52/gerenzhongxin.html?userId=27&sign=965435ff254fbcffee25c0d2b7e92dc8"); // try { // CloseableHttpResponse response = client.execute(get); // HttpEntity entity = response.getEntity();// // String result = EntityUtils.toString(entity, "UTF-8"); // System.out.println(result); // } catch (ClientProtocolException e) { // // TODO 自动生成的 catch 块 // e.printStackTrace(); // } catch (IOException e) { // // TODO 自动生成的 catch 块 // e.printStackTrace(); // } } }
总结:第三方工具,少去了流的读写。总体运用简单易懂。