httpclient的post和get,httpurlconnection的post和get



public class HttpHelp {
/**
* httpclient的get方法

* @param handler
* @param url
*/


public static void httpGet(final Handler handler, String url) {
new Thread(new Runnable() {
@Override
public void run() {
StringBuffer result = new StringBuffer();
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"http://www.douban.com/j/app/radio/channels");
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
InputStream inputStream = entity.getContent();
result = new StringBuffer();
InputStreamReader reader = new InputStreamReader(
inputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
handler.sendEmptyMessage(0);
System.out.println(result.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();


}


/**
* httpclient的post方法

* @param handler
* @param url
*/
public static void httpPost(final Handler handler, String url) {
new Thread(new Runnable() {
@Override
public void run() {
StringBuffer result = new StringBuffer();
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://www.douban.com/j/app/login");
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair("app_name",
"radio_desktop_win"));
formParams.add(new BasicNameValuePair("version", "100"));
formParams.add(new BasicNameValuePair("email",
"liuerbao@aliyun.com"));
formParams.add(new BasicNameValuePair("password", "890924"));
UrlEncodedFormEntity urlEncodedFormEntity;
try {
urlEncodedFormEntity = new UrlEncodedFormEntity(formParams,
"UTF-8");
httpPost.setEntity(urlEncodedFormEntity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
InputStream inputStream = entity.getContent();
result = new StringBuffer();
InputStreamReader reader = new InputStreamReader(
inputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
// handler.sendEmptyMessage(0);
System.out.println(result.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();


}


/**
* httpurlconnection的get方法

* @param handler
* @param url
*/
public static void preformGet(final Handler handler, String url) {
new Thread(new Runnable() {
@Override
public void run() {
StringBuffer result = new StringBuffer();
// TODO Auto-generated method stub
URL url;
try {
url = new URL("http://www.douban.com/j/app/radio/channels");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
InputStream inputStream = conn.getInputStream();
InputStreamReader in = new InputStreamReader(
inputStream);
BufferedReader reader = new BufferedReader(in);
String line;
if ((line = reader.readLine()) != null) {
result.append(line);
}
System.out.println(result);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}).start();


}


/**
* httpurlconnection的post方法

* @param handler
* @param url
*/
public static void preformPost(final Handler handler, final String urlString) {
new Thread(new Runnable() {
@Override
public void run() {
StringBuffer result = new StringBuffer();
// TODO Auto-generated method stub
URL url;
try {
url = new URL("http://www.douban.com/j/app/login");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setDoInput(true);
conn.setDoInput(true);
StringBuffer param = new StringBuffer();
param.append("app_name=").append("radio_desktop_win")
.append("&version=").append("100")
.append("&email=").append("liuerbao@aliyun.com")
.append("&password=").append("890924");
conn.getOutputStream().write(param.toString().getBytes());
if (conn.getResponseCode() == 200) {
InputStream inputStream = conn.getInputStream();
InputStreamReader in = new InputStreamReader(
inputStream);
BufferedReader reader = new BufferedReader(in);
String line;
if ((line = reader.readLine()) != null) {
result.append(line);
}
System.out.println(result);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}).start();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值