1、GET方式

其实GET方式说白了,就是拼接字符串。。最后拼成的字符串的格式是: path ? username= ....& password= ......

public boolean loginByGet(String path, String username , String password) throws Exception{String url_path = path +"?username=" + URLEncoder.encode(username, "utf-8") + "&password="+password;URL url = new URL(url_path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);if(conn.getResponseCode() == 200){return true;}return false;}


2、POST方式

post方式中,一定要设置以下两个请求参数

conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");conn.setRequestProperty("Content-Length", entity.length + "");


完整代码如下:

public boolean loginByPost(String path,String username , String password) throws Exception{System.out.println("LoginService的loginByPost()");URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("POST");conn.setConnectTimeout(5000);String value = "username=" + username +"&" + "password=" +password;        byte[] entity = value.getBytes(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");conn.setRequestProperty("Content-Length", entity.length + "");conn.setDoOutput(true);OutputStream os = conn.getOutputStream();os.write(entity);if(conn.getResponseCode() == 200){return true;}return false;}


3、通过HttpClient以GET方式提交请求

使用HttpClient这个第三方框架以后,我们在编写代码的时候就看不到URL、conn之类的。他其实就是对Http协议进行了封装

public boolean loginByHttpClientGet(String path,String username , String password) throws Exception{String value = path + "?username=" + username +"&password=" + password; HttpClient httpClient =  new DefaultHttpClient();HttpGet httpGet = new HttpGet(value);HttpResponse httpResponse = httpClient.execute(httpGet);    if(httpResponse.getStatusLine().getStatusCode() == 200){    return true;    }        return false;}



4、通过HttpClient以POST方式提交请求

   
public boolean loginByHttpClientPost(String path,String username , String password)throws Exception{HttpClient httpClient = new DefaultHttpClient();HttpPost httpPost = new HttpPost(path);List<NameValuePair> parameters = new ArrayList<NameValuePair>();parameters.add(new BasicNameValuePair("username", username));parameters.add(new BasicNameValuePair("password", password));UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"utf-8");httpPost.setEntity(entity);HttpResponse httpResponse = httpClient.execute(httpPost);    if(httpResponse.getStatusLine().getStatusCode() == 200){    return true;    }        return false;}


更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. GitHub 标星 2.5K+!教你通过玩游戏的方式学习 VIM!
  3. 分支和循环(二)(零基础学习C语言)
  4. android中使用BitmapFactory的decodeStream()方法解码图片失败问
  5. Android(安卓)eclipse 自动补全的设置
  6. android 通过intent调用短消息的正确方法
  7. Android.mk:21: *** 遗漏分隔符
  8. Android开发人员不得不收集的代码(持续更新中)(http://www.jiansh
  9. Android(安卓)- 基于Toolbar的Navigation Drawer(Material Desig

随机推荐

  1. android flutter 混合开发(配置篇)
  2. android中控件点击两次才响应onclick方法
  3. Android(安卓)Gson
  4. eclipse 安装android之经验
  5. Android recovery 模式
  6. Android使用ContentProvider报异常(java.l
  7. Android 小項目之---猜撲克牌遊戲 (附源碼
  8. Android-网络框架04Retrofit2.0+RxJava
  9. 使用SharedPreferences存储和读取数据
  10. DataBinding 的简单使用