zoukankan      html  css  js  c++  java
  • Android面向HTTP协议发送post请求

    /**
    	 * 採用post请求的方式
    	 * 
    	 * @param username
    	 * @param password
    	 * @return null表示求得的路径有问题,text返回请求得到的数据
    	 */
    	public static String postRequest(String username, String password) {
    		try {
    			String path = "http://172.22.64.156:8080/0001AndroidWebService/LoginServlet";
    			URL url = new URL(path);
    			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    			conn.setReadTimeout(500);
    			conn.setRequestMethod("POST");
    			// username=donghongyu&&password=123
    			// 准备要传输的数据
    			String data = "username=" + URLEncoder.encode(username)
    					+ "&password=" + URLEncoder.encode(password);
    			// 设置请求的内容的类型
    			conn.setRequestProperty("Content-Type",
    					"application/x-www-form-urlencoded");
    			conn.setRequestProperty("Content-Length", data.length() + "");
    
    			// 开启向server写入的权限
    			conn.setDefaultUseCaches(true);
    			// 获取http连接的输出流
    			OutputStream os = conn.getOutputStream();
    			// 向server写入数据
    			os.write(data.getBytes());
    
    			int code = conn.getResponseCode();
    			if (code == 200) {
    				// 请求成功
    				InputStream is = conn.getInputStream();
    				String text = StreamUtil.readStream(is);
    				return text;
    			} else {
    				// 请求失败
    				return null;
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    

  • 相关阅读:
    6410实现网卡(DM9000A)收发功能及ARP协议实现
    Shuffling Machine和双向链表
    Have Fun with Numbers及循环链表(约瑟夫问题)
    Tiny6410 LCD设置
    RAM与内存
    inet_addr解析
    map容器find用法
    WinSock编程(TCP)
    Python 时间序列作图及注释
    无法打开之前cuda的vs项目,打开之后变灰色
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6694925.html
Copyright © 2011-2022 走看看