HttpClient 使用
2014-04-09 14:09
821 查看
public static String inStream2String(InputStream is) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = -1;
while ((len = is.read(buf)) != -1)
{
baos.write(buf, 0, len);
}
return new String(baos.toByteArray());
}
/**第三方网络访问组件 get请求
*/
public static String getHttpClient(String urlString)
{
String resultString = "";
DebugHelper.i(TAG, urlString);
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(urlString);
try
{
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
InputStream is = response.getEntity().getContent();
resultString = HttpServer.inStream2String(is);
DebugHelper.i(TAG, "resultString:" + resultString);
}
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
return resultString;
}
/**第三方网络访问组件 post请求
List <NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
*/
public static String postHttpClient(String urlString,
List<NameValuePair> params)
{
String resultString = "";
DebugHelper.i(TAG, urlString);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
//post.setHeader("Content-Type",
//"application/x-www-form-urlencoded; charset=utf-8");
try
{
if (PingGuApplication.isDebug)
{
for (int i = 0; i < params.size(); i++)
{
DebugHelper.i(TAG, "Name:" + params.get(i).getName()
+ " Value:" + params.get(i).getValue());
}
}
//UrlEncodedFormEntity urlEncodedFormEntity=new UrlEncodedFormEntity(params, HTTP.UTF_8);
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
InputStream is = response.getEntity().getContent();
resultString = inStream2String(is);
DebugHelper.i(TAG, "resultString:" + resultString);
}
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
return resultString;
}
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = -1;
while ((len = is.read(buf)) != -1)
{
baos.write(buf, 0, len);
}
return new String(baos.toByteArray());
}
/**第三方网络访问组件 get请求
*/
public static String getHttpClient(String urlString)
{
String resultString = "";
DebugHelper.i(TAG, urlString);
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(urlString);
try
{
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
InputStream is = response.getEntity().getContent();
resultString = HttpServer.inStream2String(is);
DebugHelper.i(TAG, "resultString:" + resultString);
}
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
return resultString;
}
List <NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
*/
public static String postHttpClient(String urlString,
List<NameValuePair> params)
{
String resultString = "";
DebugHelper.i(TAG, urlString);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
//post.setHeader("Content-Type",
//"application/x-www-form-urlencoded; charset=utf-8");
try
{
if (PingGuApplication.isDebug)
{
for (int i = 0; i < params.size(); i++)
{
DebugHelper.i(TAG, "Name:" + params.get(i).getName()
+ " Value:" + params.get(i).getValue());
}
}
//UrlEncodedFormEntity urlEncodedFormEntity=new UrlEncodedFormEntity(params, HTTP.UTF_8);
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
InputStream is = response.getEntity().getContent();
resultString = inStream2String(is);
DebugHelper.i(TAG, "resultString:" + resultString);
}
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
return resultString;
}
相关文章推荐
- HttpClient详细使用
- Apache Common HttpClient使用之七种武器
- android AsyncHttpClient使用
- Android访问网络,使用HttpURLConnection还是HttpClient?
- httpclient 简单介绍使用方法
- Android 5.0无法使用HttpClient
- 网络请求之HttpClient 类的使用
- HttpClient使用HttpGet获取Json数据出现部分数据乱码的情况
- httpclient使用详解(爬虫)
- HttpClient类包基本使用方法
- 异步httpclient(httpasyncclient)的使用与总结
- HttpClient的使用
- 使用OkHttpClient的GET方式
- java httpClient使用代理实现外网访问
- httpclient 无信任证书使用https
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android httpClient.jar 在Android Studio 中的使用方法
- HttpClient 4 使用POST方式提交普通表单数据的例子
- HttpClient使用代理ip
- 如何使用HttpClient认证机制