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;
}
}
}
提交数据到服务器
最新推荐文章于 2023-06-02 10:36:02 发布