HttpURLConnection的get请求

来源:互联网 发布:艾宾浩斯记忆曲线软件 编辑:程序博客网 时间:2024/05/14 03:36

首先需要获取HttpURLConnection的实例,需要new出一个URL对象,并传入目标网址,然后调用openConnection()方法即可如下所示:

URL url = new URL("http://www.baidu.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection();

得到HttpURLConnection实例后,一般只需要new出一个URL对象,并且传入目标网络的的地址,然后调用openConnection方法就可以了。然后设置请求方法,连接超时,读取超时以及请求头等

//设置请求参数mHttpURLConnection.setRequestMethod("GET");//设置连接超时时间mHttpURLConnection.setConnectTimeout(15000);//设置读取超时时间mHttpURLConnection.setReadTimeout(15000);//添加请求头mHttpURLConnection.setRequestProperty("Connection","keep-alive");

然后使用getInputStream方法获取服务器返回的输入流,接着在读取输入流,读取完毕后使用disconnect方法将HTTP连接关闭

/获取服务器返回的输入流InputStream inputStream = mHttpURLConnection.getInputStream();//读取输入流sread = new BufferedReader(new InputStreamReader(inputStream));

完整代码如下:

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button button = (Button)findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                new Thread(new Runnable() {                    @Override                    public void run() {                        HttpURLConnection mHttpURLConnection = null;                        BufferedReader read = null;                        try {                            URL url = new URL("https://www.baidu.com");                            mHttpURLConnection = (HttpURLConnection) url.openConnection();                            //设置请求参数                            mHttpURLConnection.setRequestMethod("GET");                            //设置连接超时时间s                            mHttpURLConnection.setConnectTimeout(15000);                            //设置读取超时时间                            mHttpURLConnection.setReadTimeout(15000);                            //添加请求头                            mHttpURLConnection.setRequestProperty("Connection","keep-alive");                            //获取服务器返回的输入流                            InputStream inputStream = mHttpURLConnection.getInputStream();                            //读取输入流s                            read = new BufferedReader(new InputStreamReader(inputStream));                            StringBuilder respose = new StringBuilder();                            String line;                            while((line = read.readLine())!=null){                                respose.append(line);                            }                            Log.d("TAG",respose.toString());                        } catch (Exception e) {                            e.printStackTrace();                        }finally {                            if(read!=null){                                try {                                    read.close();                                } catch (IOException e) {                                    e.printStackTrace();                                }                            }                            if(mHttpURLConnection!=null){                                //请求结束后关闭HTTP链接                                mHttpURLConnection.disconnect();                            }                        }                    }                }).start();            }        });    }}
原创粉丝点击