android post get请求服务器

get 请求方法

public class ForGetRequest {
    
    public static Boolean sendParam(String path,Map<String,String> map) throws IOException{
        StringBuilder sb = new StringBuilder(path);
        sb.append("?");
        for(Map.Entry<String, String> entry:map.entrySet()){
            sb.append(entry.getKey()+"="+entry.getValue()+"&");
        }
        sb.deleteCharAt(sb.length()-1);
        System.out.println(sb.toString());
        URL url = new URL(sb.toString());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setReadTimeout(5*1000);
        if(conn.getResponseCode()==200){
            return true;
        }else{
            return false;
        }
    }
}

post请求方法1

public class SendMessage {
    //通过HttpClient组件进行post请求。SSL。
    public static boolean sendMsgFromHttpClient(Map<String,String> map,String path,String encoding) throws IOException{
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        for(Map.Entry<String, String> entry:map.entrySet()){
            list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        UrlEncodedFormEntity form = new UrlEncodedFormEntity(list,encoding);
        HttpPost post = new HttpPost(path);
        post.setEntity(form);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse request = client.execute(post);
        if(request.getStatusLine().getStatusCode()==200){
            return true;
        }
        return false;
    }
    //这种post不是ssl,但请求速度比上面方法快
    public static boolean postMsg(Map<String,String> map,String path,String encoding) throws IOException{
        //Content-Type: application/x-www-form-urlencoded
        //Content-Length: 15
        StringBuilder sb = new StringBuilder();
        if(map!=null && !map.isEmpty() ){
            for(Map.Entry<String, String> params:map.entrySet()){
                sb.append(params.getKey()).append("=").append(params.getValue()).append("&");
            }
            sb.deleteCharAt(sb.length()-1);
        }
        byte[] b = sb.toString().getBytes();
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setReadTimeout(5000);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length",String.valueOf(b.length));
        OutputStream out = conn.getOutputStream();
        out.write(b);
        out.flush();
        out.close();
        if(conn.getResponseCode()==200){
            return true;
        }
        return false;
    }
}

 

 

转载于:https://www.cnblogs.com/flyyl/p/3793721.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值