HttpClient联网请求工具类

package com.example.abnerming.httputil.net;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HttpClientHelper {
public HttpClientHelper(){}

public void get(){
    HttpClient client=new DefaultHttpClient();
    HttpGet httpGet=new HttpGet("");
    try {
        HttpResponse httpResponse=client.execute(httpGet);
        int code=httpResponse.getStatusLine().getStatusCode();
        if(code==200){
            httpResponse.getEntity().getContent();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void post(){
    HttpClient httpClient=new DefaultHttpClient();
    HttpPost httpPost=new HttpPost("");
    try {
        List<BasicNameValuePair> list=new ArrayList<>();
        BasicNameValuePair basicNameValuePair=new BasicNameValuePair("","");
        UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,"UTF-8");
        httpPost.setEntity(entity);
        HttpResponse response=httpClient.execute(httpPost);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

猜你喜欢

转载自blog.csdn.net/weixin_43191402/article/details/82666786