简单的 URL 编程
2016-05-20 20:37
253 查看
1.服务器端,访问则返回“你好”
2.客户端
@WebServlet("/myServlet") public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream outputStream = response.getOutputStream(); String message = "你好"; byte[] bytes = message.getBytes(); System.out.println(bytes.length); outputStream.write(bytes); }
2.客户端
public class Client { public static void main(String[] args) { try { HttpURLConnection connection = (HttpURLConnection) new URL("http://192.168.78.111:8080/kok/loginservlet") .openConnection(); InputStream inputStream = connection.getInputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int len = 0; byte [] b = new byte[10]; while((len = inputStream.read(b)) != -1){ outputStream.write(b, 0, len); } byte[] byteArray = outputStream.toByteArray(); for(int i = 0; i < byteArray.length; i++ ){ System.out.println(byteArray[i]); } String string = new String(byteArray); System.out.println(string); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- Lua下基本的网络编程示例
- linux网络编程用到的网络函数详解用和使用示例
- C#网络编程基础之进程和线程详解
- C++ 网络编程 总结
- Linux网络编程之UDP Socket程序示例
- Android网络编程之UDP通信模型实例
- 谈谈新手如何学习PHP网络编程第1/2页
- Python 网络编程起步(Socket发送消息)
- java必学必会之网络编程
- c# socket网络编程接收发送数据示例代码
- NodeJS学习笔记之网络编程
- Java网络编程之简单的服务端客户端应用实例
- java网络编程之识别示例 获取主机网络接口列表
- Python 网络编程说明第1/2页
- 详解Python Socket网络编程
- python网络编程学习笔记(一)
- python socket网络编程步骤详解(socket套接字使用)
- python网络编程之TCP通信实例和socketserver框架使用例子
- Android网络编程之获取网络上的Json数据实例
- Java网络编程基础教程之Socket入门实例