博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
远程访问
阅读量:6305 次
发布时间:2019-06-22

本文共 2251 字,大约阅读时间需要 7 分钟。

HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet(baseUrl + action);
HttpResponse response = httpclient.execute(httpget);
retStr = EntityUtils.toString(response.getEntity());

 

JSONObject objRoot = new JSONObject(retStr );

retStr = objRoot.getString("qwe");

所用到的jar 包:httpcore httpclient

 

 

public static String sendPost(String host,String content,String encodeType) throws UnsupportedEncodingException,IOException

{
HttpClient httpClient = new DefaultHttpClient();
HttpPost post= new HttpPost(host);

//NameValuePair p = new BasicNameValuePair("v", "123");

List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("v", "1405322466697"));
StringEntity entity = new UrlEncodedFormEntity(list);
post.setEntity(entity);

StringEntity myEntity;
if(!encodeType.isEmpty())
myEntity = new StringEntity(content,encodeType);
else
myEntity = new StringEntity(content);
post.addHeader("Content-Type", "text/xml");
post.addHeader("Connection","keep-alive");
post.addHeader("Accept","*/*");
post.addHeader("Cache-Control", "max-age=0");
post.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
post.setEntity(myEntity);
HttpResponse response = httpClient.execute(post);
String answerRes = "";
if(response.getStatusLine().getStatusCode()==200)
{
//如果状态码为200,就是正常返回
String result=EntityUtils.toString(response.getEntity());
answerRes = new String(result.getBytes("iso-8859-1"), "UTF-8");
}
return answerRes;
}
public static String sendGet(String url) throws UnsupportedEncodingException,IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpGet get= new HttpGet(url);
get.addHeader("Connection","keep-alive");
get.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8");
get.addHeader("Cache-Control", "max-age=0");
get.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
HttpResponse response = httpClient.execute(get);
String answerRes = "";
if(response.getStatusLine().getStatusCode()==200)
{
//如果状态码为200,就是正常返回
String result=EntityUtils.toString(response.getEntity());
answerRes = new String(result.getBytes("iso-8859-1"), "UTF-8");
}
return answerRes;
}

转载于:https://www.cnblogs.com/Wen-yu-jing/p/3767233.html

你可能感兴趣的文章
JavaScript强化教程 —— Cocos2d-JS自动JSB绑定规则修改
查看>>
configure: error: in `/root/httpd-2.2.11/srclib/apr': c
查看>>
CentOS7搭建Kubernetes-dashboard管理服务
查看>>
buildroot下查找外部编译器通过ext-toolchain-wrapper调用的参数
查看>>
MySQL Replication 主主配置详细说明
查看>>
Linux的任务调度
查看>>
在Android studio中添加jar包方法如下
查看>>
iframe 在ie下面总是弹出新窗口解决方法
查看>>
分享10款漂亮实用的CSS3按钮
查看>>
安装nginx 常见错误及 解决方法
查看>>
Gorun8电子商城
查看>>
在之前链表的基础上改良的链表
查看>>
android编译系统makefile(Android.mk)写法
查看>>
MD5源代码C++
查看>>
Eclipse 添加 Ibator
查看>>
Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
查看>>
Python编程语言
查看>>
十四、转到 linux
查看>>
Got error 241 'Invalid schema
查看>>
ReferenceError: event is not defined
查看>>