* 到服务器验证登录信息,通过get方法
*/
public String getDataByServerByGet(String name,String pass){
String rs="";
URL url=null;
HttpURLConnection urlConn=null;
BufferedReader br=null;
StringBuffer sb=new StringBuffer(LOGIN_URL);//创建缓存字符流,拼装字符串
sb.append("?username="+name);
sb.append("&userpass="+pass);
System.out.println(sb.toString());
try {
url=new URL(sb.toString());
urlConn=(HttpURLConnection) url.openConnection();//打开连接
urlConn.setConnectTimeout(5*1000);
urlConn.setReadTimeout(5*1000);
if(urlConn.getResponseCode()==200){//响应成功
br=new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8"));
rs=br.readLine();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/*
* 到服务器验证登录信息,通过 post请求
*/
public String getDataByServerByPost(String name,String pass){
String rs="";
URL url=null;
HttpURLConnection urlConn=null;
BufferedReader br=null;
DataOutputStream dos=null;
try {
url=new URL(LOGIN_URL);
urlConn=(HttpURLConnection) url.openConnection();//打开连接
urlConn.setConnectTimeout(5*1000);
urlConn.setReadTimeout(5*1000);
urlConn.setRequestMethod("POST");//设置请求方法
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
//通过post方式向服务器传值
dos=new DataOutputStream(urlConn.getOutputStream());//得到服务器的输出流
String data="username="+name+"&userpass="+pass;
dos.write(data.getBytes());
dos.flush();
dos.close();//传值完毕
if(urlConn.getResponseCode()==200){//响应成功
br=new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8"));
rs=br.readLine();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}