码迷,mamicode.com
首页 > Web开发 > 详细

http请求

时间:2016-10-27 02:06:24      阅读:209      评论:0      收藏:0      [点我收藏+]

标签: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请求

标签:http请求

原文地址:http://7915791.blog.51cto.com/7905791/1865964

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!