Http的连接方式之HttpUrlConnection
2015-08-13 22:03
645 查看
doget方式
String urlString = "http://localhost:8080/MyWebApp/MyServlet?username=张三&password=123456"; try { URL url = new URL(urlString); URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connection; httpConnection.setRequestMethod("GET"); // 设置接受的数据类型 httpConnection.setRequestProperty("Accept-Charset", "utf-8"); // 设置可以接受序列化的java对象 httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置连接超时的时间 httpConnection.setConnectTimeout(3000); //设置读取超时 httpConnection.setReadTimeout(3000); int code =httpConnection.getResponseCode(); System.out.println("http状态码:"+code); if(code==HttpURLConnection.HTTP_OK){ InputStream is= httpConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); while(line!=null){ System.out.println(line); line=br.readLine(); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch(SocketTimeoutException e){ System.out.println("网络连接超时"); } catch (IOException e) { e.printStackTrace(); }
dopost方式
String urlString = "http://localhost:8080/WebServer/MyServletJSON"; try { URL url = new URL(urlString); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); /** * post和get的不同点 */ //设置提交方法为post httpConnection.setRequestMethod("POST"); //设置可以可以获取服务器返回的内容,默认为true // httpConnection.setDoInput(true); //设置客户端可以给服务器提交数据,默认是false的。post方法必须为true httpConnection.setDoOutput(true); //post方法不允许使用缓存 httpConnection.setUseCaches(false); /** * */ // 设置接受的数据类型 httpConnection.setRequestProperty("Accept-Charset", "utf-8"); // 设置可以接受序列化的java对象 httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 设置连接超时的时间 // httpConnection.setConnectTimeout(3000); // // 设置读取超时 // httpConnection.setReadTimeout(3000); /** * 设置向服务器提交的参数 */ String params = "jsoncontent=zhangsan"; httpConnection.getOutputStream().write(params.getBytes()); int code = httpConnection.getResponseCode(); System.out.println(code); if(code==HttpURLConnection.HTTP_OK){ InputStream is = httpConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); while(line!=null){ System.out.println(line); line = br.readLine(); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
相关文章推荐
- 可靠联机的 TCP 协议
- 可靠联机的 TCP 协议
- 【工业串口和网络软件通讯平台(SuperIO)教程】六.二次开发导出数据驱动
- 【工业串口和网络软件通讯平台(SuperIO)教程】六.二次开发导出数据驱动
- JSON格式注册登录查询byHttpClientDoPost
- 深入理解TCP(二)
- 8月12日---URLConnection,HTTPClient
- linux下网络编程2:服务端和客户端进行TCP通信实例
- Android使用ImageLoader异步加载网络图片(二)结合listview
- TCP的状态兼谈Close_Wait和Time_Wait的状态
- 网络模型各层报头
- Android-应用开发-网络编程(四)
- Snail—iOS网络学习之得到网络上的数据
- 【Android 】HttpURLConnection里byte转化string的错误笔记
- Android使用ImageLoader异步加载网络图片(一)读取单张图片
- SOAPUI 使用groovy发送http请求 自动化测试
- 快速解决局域网网络环路问题
- http://zzgthk.iteye.com/blog/1064434
- TCP连接
- Android-应用开发-网络编程(五)