首页 > 代码库 > http请求
http请求
简单http请求发送,可以自己设置contentType,跨域等信息
importjava.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; /** * Http工具 */ public class HttpUtil { /** * 简单post文本 * @param url 请求地址 * @param text 请求内容 */ public static String simplePost( String url , String text ){ try { CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpPost httppost = new HttpPost(url ); StringEntity se = new StringEntity( text ); se.setContentType( "text/xml;charset=utf-8" ); httppost.setEntity( se ); httppost.addHeader( "Content-Type","application/json" ); CloseableHttpResponse response = httpclient.execute( httppost ); HttpEntity entity = response.getEntity(); String html = EntityUtils. toString( entity, "utf-8" ); httppost.releaseConnection(); return html ; } catch (Exception e ) { throw new RuntimeException( e ); } } /** * 简单post文本 * @param url 请求地址 * @param text 请求内容 */ public static String simplePost( String url , String text, Map<String, String> headMap ){ try { CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpPost httppost = new HttpPost(url ); StringEntity se = new StringEntity( text ); se.setContentType( "text/xml;charset=utf-8" ); httppost.setEntity( se ); if( headMap != null ){ for( String hk : headMap .keySet() ){ httppost.addHeader( hk, headMap.get(hk ) ); } } CloseableHttpResponse response = httpclient.execute( httppost ); HttpEntity entity = response.getEntity(); String html = EntityUtils. toString( entity, "utf-8" ); httppost.releaseConnection(); return html ; } catch (Exception e ) { throw new RuntimeException( e ); } } /** * ajax 请求get 方式 * @param url * @return */ public static String simpleGet( String url ){ try { CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpGet httpget = new HttpGet(url ); httpget.setHeader( "Access-Control-Allow-Origin","*" ); CloseableHttpResponse response = httpclient.execute( httpget ); HttpEntity entity = response.getEntity(); String html = EntityUtils. toString( entity, "utf-8" ); httpget.releaseConnection(); return html ; } catch (Exception e ) { throw new RuntimeException( e ); } } }
本文出自 “my dream fly on the sky” 博客,请务必保留此出处http://7915791.blog.51cto.com/7905791/1865964
http请求
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。