提交数据到服务器

public class NetUtils {
	/**
	 * 使用get方式提交服务器获得 返回值信息
	 * 
	 * @param username
	 * @param userpwd
	 * @return
	 */
	public static String dogetinfo(String username, String userpwd) {
		HttpURLConnection conn = null;
		try {

			URL url = new URL(
					"http://10.0.2.2:8080/demo/servlet/login?username="
							+ username + "&userpwd=" + userpwd);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(10000);
			conn.setReadTimeout(5000);
			int code = conn.getResponseCode();
			if (code == 200) {
				InputStream is = conn.getInputStream();
				return getStringFromInputStream(is);

			} else {
				Log.i("ssss", "访问失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
		}

		return null;
	}

	/**
	 * 使用post方式提交服务器获得 返回值信息
	 * 
	 * @param username
	 * @param userpwd
	 * @return
	 */
	public static String dopostinfo(String username, String userpwd) {
		HttpURLConnection conn = null;
		try {

			URL url = new URL("http://10.0.2.2:8080/demo/servlet/login");
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setConnectTimeout(10000);
			conn.setReadTimeout(5000);
			String data = "username=" + username + "&userpwd=" + userpwd;
			OutputStream out = conn.getOutputStream();
			out.write(data.getBytes());
			out.flush();
			out.close();

			int code = conn.getResponseCode();
			if (code == 200) {
				InputStream is = conn.getInputStream();
				return getStringFromInputStream(is);

			} else {
				Log.i("ssss", "访问失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
		}

		return null;
	}

	/**
	 * 使用httpclientget方式提交服务器获得 返回值信息  
	 * 
	 * @param username
	 * @param userpwd
	 * @return
	 */
	public static String dohttpclientgetinfo(String username, String userpwd) {
		HttpClient client = null;
		try {
			// 定义一个客户端
			client = new DefaultHttpClient();
			String data = "username=" + username + "&userpwd=" + userpwd;
			// 定义一个httpget 当做get请求方法
			HttpGet get = new HttpGet(
					"http://10.0.2.2:8080/demo/servlet/login?" + data);
			HttpResponse response = client.execute(get);// 开始执行get请求网络 这里
														// 保护返回信息和状态
			int statecodse = response.getStatusLine().getStatusCode();
			if (statecodse == 200) {
				InputStream is = response.getEntity().getContent();
				return getStringFromInputStream(is);
			} else {
				Log.i("错误", "错误");
			}
		} catch (Exception e) {

		} finally {
			if (client != null) {
				client.getConnectionManager().shutdown();// 关闭连接 释放资源
			}
		}

		return null;
	}

	/**
	 * 使用httpclientpost方式提交服务器获得 返回值信息
	 * 
	 * @param username
	 * @param userpwd
	 * @return
	 */
	public static String dohttpclientpost(String username, String userpwd) {
		HttpClient client = null;
		try {
			// 定义一个客户端
			client = new DefaultHttpClient();
			String data = "username=" + username + "&userpwd=" + userpwd;
			// 定义一个httppost 当做post请求方法
			HttpPost post = new HttpPost(
					"http://10.0.2.2:8080/demo/servlet/login");

			List<NameValuePair> parameters = new ArrayList<NameValuePair>();//定义参数list
			parameters.add(new BasicNameValuePair("username", username));//添加参数
			parameters.add(new BasicNameValuePair("userpwd", userpwd));
			HttpEntity entity = new UrlEncodedFormEntity(parameters);//增加参数
			post.setEntity(entity);//为post添加参数
			HttpResponse response = client.execute(post);// 开始执行post请求网络 这里
															// 保护返回信息和状态
			int statecodse = response.getStatusLine().getStatusCode();
			if (statecodse == 200) {
				InputStream is = response.getEntity().getContent();
				return getStringFromInputStream(is);
			} else {
				Log.i("错误", "错误");
			}
		} catch (Exception e) {

		} finally {
			if (client != null) {
				client.getConnectionManager().shutdown();// 关闭连接 释放资源
			}
		}
		return null;
	}

	private static String getStringFromInputStream(InputStream is)
			throws IOException {
		{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = -1;
			while ((len = is.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			is.close();
			String html = new String(baos.toByteArray(), "GBK");
			return html;

		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第一种方法 使用标准的JAVA接口 1 将用户名和密码等用Map泛型封装 再使用StringBuffer 转换成一串字符串 然后新建URL打开openConnection 得到 httpURLConnection 设置最长连接时间和setRequestMethod请求方法 用GET还是POST 提交数据用POST 打开输入和输出流 获取上传信息 字节大小以及长度 设置请求体的类型是文本类型 获得输出流 向服务器输出数据 获得服务器响应的结果和状态码 如果 返回码等于200 得到服务器返回的输入流 将输入流转换成指定编码的字符串并返回 就可以成功提交并得到服务器返回的信息 第二种方法 使用标准Apache接口 02 03 15 19 30 + 06 12 02 03 15 19 30 31 + 06 12 1 将用户名和密码等用Map泛型封装 再使用List<NameValuePair> list new ArrayList<NameValuePair> ; 转换成一串字符串 然后新建URL打开openConnection 得到 httpURLConnection 设置最长连接时间和setRequestMethod请求方法 用GET还是POST 提交数据用POST 打开输入和输出流 获取上传信息 字节大小以及长度 设置请求体的类型是文本类型 获得输出流 向服务器输出数据 获得服务器响应的结果和状态码 如果 返回码等于200 得到服务器返回的输入流 将输入流转换成指定编码的字符串并返回 就可以成功提交并得到服务器返回的信息">第一种方法 使用标准的JAVA接口 1 将用户名和密码等用Map泛型封装 再使用StringBuffer 转换成一串字符串 然后新建URL打开openConnection 得到 httpURLConnection 设置最长连接时间和setRequestMethod请求方法 用GET还是POST 提交数据用POST [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值