HttpClient doPost doGet 请求 返回内存流

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 21:02   4400   0

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.ObjectOutputStream;

import java.io.OutputStream;

import java.io.UnsupportedEncodingException;

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.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.ContentProducer;

import org.apache.http.entity.EntityTemplate;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;


/**

* http invoker for doPost request or doGet request, return memory stream

* <code>OutputStream</code>,ie. <code>ByteArrayOutputStream</code>

*

*

* @author

* @see org.apache.http.client.HttpClient

*/

public class HttpInvoker

{


/**

* doGet方式访问URL

*

* @param url

* @return OutputStream

*/

public static OutputStream doGet(String url)

{

HttpClient httpclient = new DefaultHttpClient();

OutputStream os = null;

try

{

HttpGet httpget = new HttpGet(url);

HttpResponse response = httpclient.execute(httpget);

HttpEntity entity = response.getEntity();


if (entity != null)

{

InputStream instream = entity.getContent();

os = new ByteArrayOutputStream();

int temp = 0;

while ((temp = instream.read()) != -1)

{

os.write(temp);

}


os.flush();

os.close();

return os;

}

}

catch (Exception e)

{

}

finally

{

httpclient.getConnectionManager().shutdown();

}

return null;


}<\]JN[ [[OH \N[]\[ [[OH \N[O [ [[OH \N[O [ [[OH \N[ ^[JO [ [[OH \N[KKH KO [ [[OH \N[O [ [[OH \N[[[O [ [[OH \N[KKH KO [ [[OH \N[Y[ X[X[Y K] N[ [[OH \N[O [ [[OH \N[]\[[ [[OH \N[O [ [[OH \N[O [ ] ]

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP