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;
    	}
    

  • 相关阅读:
    让Oracle高效并行执行的13个必备知识点
    oracle使用并行踩过的坑
    oracle parellel 案例
    并行查询 最基本
    ORACLE parallel 3个层面的影响
    Mac下百度网盘破解
    vscode整个项目的查找替换快捷键
    mysql---group_concat
    mysql-group by 与 where
    npm安装指定版本包
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6694925.html
Copyright © 2011-2022 走看看