java通过Http get post请求url获取数据
2017-06-28 13:53
976 查看
package com.avic.core.utils; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSONObject; public class HttpGetRequest { /** * Get Request * * @return * @throws Exception */ public static String doGet(String url) throws Exception { URL localURL = new URL(url); URLConnection connection = localURL.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection) connection; httpURLConnection.setRequestProperty("Accept-Charset", "utf-8"); httpURLConnection.setRequestProperty("Content-Type", "application/text"); InputStream inputStream = null; InputStreamReader inputStreamReader = null; BufferedReader reader = null; StringBuffer resultBuffer = new StringBuffer(); String tempLine = null; if (httpURLConnection.getResponseCode() >= 300) { throw new Exception( "HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode()); } try { inputStream = httpURLConnection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); reader = new BufferedReader(inputStreamReader); while ((tempLine = reader.readLine()) != null) { resultBuffer.append(tempLine); } } finally { if (reader != null) { reader.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (inputStream != null) { inputStream.close(); } } return resultBuffer.toString(); } /** * Http post * @param url post请求url * @param params 参数 * @return * @throws Exception */ public static String doPost(String url,Map<String, String> params) throws Exception { HttpPost httpPost=new HttpPost(url); HttpClient httpclient=new DefaultHttpClient(); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if(params!=null){ BasicNameValuePair bnvp=null; for (Map.Entry<String, String> p : params.entrySet()) { bnvp=new BasicNameValuePair(p.getKey(), p.getValue()); } } httpPost.setEntity(new UrlEncodedFormEntity(nvps)); HttpResponse response = httpclient.execute(httpPost); HttpEntity respEntity = response.getEntity();//获得返回数据 String text = EntityUtils.toString(respEntity, "UTF-8"); JSONObject obj = (JSONObject) JSONObject.parse(text); httpclient.getConnectionManager().shutdown(); return obj.toJSONString(); } }
相关文章推荐
- 使用HttpURLConnection通过GET、POST请求数据
- 通过get或post请求URL获取返回数据
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- java使用HttpURLConnection和HttpClient分别模拟get和post请求以及操作cookies
- android(21)(使用HttpURLConnection,HttpClient和AsyncHttpClient分别实现post和get请求数据)
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- java HTTP get post请求,获取二进制文件实现
- HTTP请求协议 get post方法 获取数据解析
- 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换
- 使用HttpURLConnection采用get方式或post方式请求数据
- Java 通过HttpURLConnection Post方式提交json,并从服务端返回json数据
- android http数据通信 通过GET 请求获取数据
- HTTP 请求方式: GET和POST的比较当发送数据时,GET 方法向 URL 添加数据;URL 的长度是受限制的(URL 的最大长度是 2048 个字符)。
- 通过Http协议以Get和Post方式获取服务器端文本数据
- HttpURLConnection和HttpClient分别以GET和POST获取Json数据
- java发送post和get请求(1)---HttpURLConnection方式
- 通过HTTP方式获取URL返回结果数据(Java)
- java HttpUrlConnection get和post 模拟请求 调用接口
- Java 通过HttpURLConnection Post方式提交xml,并从服务端返回数据
- HttpUrlConnection两种请求数据的方式get和post提交数据