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(); } }); }}
阅读全文
0 0
- HttpURLConnection 的get请求
- HttpURLConnection的get请求
- HttpURLConnection的get请求
- HttpURLConnection--HttpURLConnection的Get请求方式
- HttpURLConnection的get请求总结
- HttpUrlConnection的get请求方式
- HttpUrlConnection的GET请求和POST请求
- httpurlconnection的Get请求和Post请求
- httpurlconnection类的get请求的用法
- HttpURLConnection 的get跟post请求
- HttpURLConnection的get和post请求
- HttpURLConnection的GET和POST请求
- HttpURLConnection的get和post请求实例
- 【Java】HttpURLConnection,HttpClient 的 Get,Post 请求
- HttpUrlConnection的get和post请求方法
- HttpURLConnection的GET和POST请求方式
- 可靠的HttpUrlConnection实现get/post请求
- Java HttpURLConnection的post和get请求
- 详解匿名内部类
- 欢迎使用CSDN-markdown编辑器
- table 搜索内容
- 394 decode string
- HTTP Session
- HttpURLConnection的get请求
- Can Place Flowers
- hdu 1863 畅通工程(MST,prim)
- 区分高通和MTK平台
- cookie设置,获取,删除。
- JAVA集合
- JAVA发送http get/post请求,调用http接口、方法
- Slim Span 求一颗生成树 使得最大边与最小边的差值最小
- SyntaxError: EOL while scanning string literal问题及解决方法