java_utils__TzHttpClient

1、

package z_utils;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

// ZC: 修改于 “20161028 16:15”

@SuppressWarnings("deprecation")
public class TzHttpClient
{
    public static void main(String[] args) throws Exception
    {
        String strRtn = PostZ(
            "http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-2.html",
            null,
            true);
        System.out.println(strRtn);
    }
    
// ***

    public static String PostZ(String _strUrl, String _strParam, boolean _bNeedResponse) throws Exception
    {
        DefaultHttpClient httpClient = null;
        try
        {
            //post请求返回结果
            httpClient = new DefaultHttpClient();
            HttpPost method = new HttpPost(_strUrl);
            if (null != _strParam)
            {
                //解决中文乱码问题
                StringEntity entity = new StringEntity(_strParam, "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                method.setEntity(entity);
            }
            HttpResponse result = httpClient.execute(method);
            /**请求发送成功,并得到响应**/
            if (result.getStatusLine().getStatusCode() == 200)
            {
                if (! _bNeedResponse)
                    return null;
                String str = EntityUtils.toString(result.getEntity());
                //System.out.println(str);
                return str;
            }
            return null;
        }
        finally
        {
            if (httpClient != null)
                httpClient.close();
        }
    }

    public static String GetZ(String _strUrl) throws Exception
    {
        DefaultHttpClient client = null;
        try
        {
            client = new DefaultHttpClient();
            //发送get请求
            HttpGet request = new HttpGet(_strUrl);
            HttpResponse response = client.execute(request);
    
            /**请求发送成功,并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                /**读取服务器返回过来的json字符串数据**/
                String strResult = EntityUtils.toString(response.getEntity());
                //System.out.println(strResult);
                return strResult;
            }
            System.out.println("get请求提交失败:" + _strUrl);
            return null;
        }
        finally
        {
            if (client != null)
                client.close();
        }
    }
}

 

2、

3、

 

posted @ 2016-10-19 21:44  CodeHouse  阅读(216)  评论(0编辑  收藏  举报