HTTP通讯请求工具包

package tools;


import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;
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.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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;


public class HttpUtil {

//path :请求地址

public static String path = " http://192.168.43.74:8080/New_Word_/servlet/";


// public static String
// prictrue_path="http://10.0.2.2:8080/News_Service/drawable/";


public static String doGet(String url, Map<String, Object> map) {
String info = null;
StringBuilder builder = new StringBuilder(url);
builder.append("?");
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object values = entry.getValue();
builder.append(key).append("=").append(values.toString())
.append("&");
}


builder.deleteCharAt(builder.length() - 1);


try {
HttpClient client = new DefaultHttpClient();


HttpGet get = new HttpGet(builder.toString());
HttpResponse response = client.execute(get);


int code = response.getStatusLine().getStatusCode();
if (code == 200) {
InputStream is = response.getEntity().getContent();


info = StreamUtil.InputSteamToString(is);
}
} catch (Exception e) {
e.printStackTrace();
}
return info;
}


public static String doPost(String url, Map<String, Object> map) {
String info = null;
try {
HttpClient client = new DefaultHttpClient();


HttpPost post = new HttpPost(url);


List<NameValuePair> oList = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
NameValuePair pair = new BasicNameValuePair(entry.getKey(),
entry.getValue().toString());


oList.add(pair);
}


HttpEntity entity = new UrlEncodedFormEntity(oList, "UTF-8");
post.setEntity(entity);


HttpResponse response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
InputStream is = response.getEntity().getContent();


info = StreamUtil.InputSteamToString(is);
}


} catch (Exception e) {
e.printStackTrace();
}
return info;
}


public static String sendGetRequest2(String path, Map<String, String> params)
throws Exception {
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, "utf-8");
HttpPost httpPost = new HttpPost(path); 
httpPost.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
InputStream in = response.getEntity().getContent();
int len;
byte[] buffer = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = in.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
String s = bos.toString();
System.out.println(s + "------");
return URLDecoder.decode(s, "utf-8");
} else {
return "error";
}
}

}







package tools;


import java.io.IOException;
import java.io.InputStream;


public class StreamUtil {



public static String InputSteamToString(InputStream is){
String info=null;
try {
StringBuilder builder=new StringBuilder();
int has=0;
byte[] buffer=new byte[1024];
while ((has=is.read(buffer))!=-1) {
builder.append(new String(buffer,0,has));
}
is.close();

info=builder.toString();

} catch (IOException e) {
e.printStackTrace();
}

return info;
}
}







/**

*请求实例

*/

new Thread() {
public void run() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("username", values0);
map.put("password", values1);
str0 = HttpUtil.doGet(HttpUtil.path
+ "Register_User", map);
handler.sendEmptyMessage(0x123);
};
}.start();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值