HttpGet 请求(带参数)
package com.example.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
public class MyUtils {
private static InputStream result;
public static InputStream getJson1FromWeb(String url) {
HttpGet get = new HttpGet(url);
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
HttpConnectionParams.setSoTimeout(params, 5 * 1000);
HttpClient client = new DefaultHttpClient(params);
HttpResponse response;
try {
response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entity.getContent();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static String getJson2FromWeb(String url, String id, int p) {
StringBuffer sb = new StringBuffer(url);
sb.append("&catalog_id=" + id);
sb.append("&pn=" + p);
String data = "";
// 创建请求对象
HttpGet httpGet = new HttpGet(sb.toString());
// 设置参数
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
// 执行请求的对象
HttpClient client = new DefaultHttpClient(params);
// 执行请求镀锡
try {
HttpResponse resp = client.execute(httpGet);
// 判断是否响应成功
if (resp.getStatusLine().getStatusCode() == 200) {
// 获取响应 的数据
HttpEntity entity = resp.getEntity();
data = EntityUtils.toString(entity, "utf-8");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
public static String getWeb(String path){
URL url=null;
HttpURLConnection connection=null;
BufferedReader br=null;
StringBuffer sb=new StringBuffer();
try {
url=new URL(path);
connection=(HttpURLConnection) url.openConnection();
if(connection.getResponseCode()==200){
br=new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String str=null;
while((str=br.readLine())!=null){
sb.append(str);
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
}
HttpGet 请求(带参数)的更多相关文章
- httpclient post请求带参数返回数据乱码问题解决
客户端代码: //带参数的post请求 @Test public void doPostWithParam() throws Exception { CloseableHttpClient httpC ...
- perl post 请求带参数
my $url='https://wenjinbao.winfae.com/business/dispatch_post.do?action=submitAdminLogin'; my $res ...
- Java后端发出post请求带参数并接收返回的json
核心代码: 参数格式: “key1=value1&key2=value2” /*** sendUrl (远程请求的URL)* param (远程请求参数)* JSONObject ...
- java get请求带参数报错 java.io.IOException: Server returned HTTP response code: 400 for URL
解决方案 在使用JAVA发起http请求的时候,经常会遇到这个错误,我们copy请求地址在浏览器中运行的时候又是正常运行的,造成这个错误的原因主要是因为请求的URL中包含空格,这个时候我们要使用URL ...
- nodejs http post 请求带参数
// We need this to build our post string var querystring = require('querystring'); var http = requir ...
- 路由传值及获取参数,路由跳转,路由检测,this.$route.query和this.$route.params接收参数,HttpGet请求拼接url参数
配置动态路由参数id: routes: [ // 动态路径参数 以冒号开头 { path: '/user/:id', component: User } ] html路由跳转: <router- ...
- 【笔记】Asp.Net WebApi对js POST带参数跨域请求的支持方案
先说下需求:在原来的WebApi项目中增加对js跨域的请求支持,请求方式:以POST为主,webapi路由规则根据原项目需求修改如下: public static void Register(Http ...
- 微信小程序之跳转、请求、带参数请求小例子
wx.request(OBJECT) wx.request发起的是 HTTPS 请求.一个微信小程序,同时只能有5个网络请求连接. 具体参数说明参看微信小程序官方文档-发起请求. 例: //当页面加载 ...
- Spring MVC中forward请求转发2种方式(带参数)
Spring MVC中forward请求转发2种方式(带参数) http://www.51gjie.com/javaweb/956.html
- angular4 get,post请求(带参数,与不带参数)
一:在app.module.ts引入HttpMoudle import { BrowserModule } from '@angular/platform-browser'; import { Htt ...
随机推荐
- 二进制mysql5.7.16下载地址
下载地址:http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz 2. 解压 tar xx ...
- 【5集iCore3_ADP演示视频】5-5 iCore3应用开发平台示波器和信号源校准
iCore3双核心应用开发平台基于iCore3双核心板,包含ARM.FPGA.7寸液晶屏.双通道数字示波器.任意波发生器.电压表等模块,是一款专为电子爱好者设计的综合性电子学习系统. [视频简介]本视 ...
- hibernate学习(9)——日志,一对一,二级缓存
1.Hibernate中的日志 1 slf4j 核心jar : slf4j-api-1.6.1.jar .slf4j是日志框架,将其他优秀的日志第三方进行整合. 整合导入jar包 log4j 核心 ...
- 关于swap函数传值的问题
#include <stdio.h> void swap(int * p3,int * p4); int main() { int a = 9; int b = 8; int * p ...
- 转摘 MySQL扫盲篇
一下文章摘自:http://www.jellythink.com/archives/636 MySQL扫盲篇 2014-09-15 分类:MySQL / 数据库 阅读(1412) 评论(1) 为什么 ...
- An error occurred while processing an SVN command
今天在使用SmartSVN的时候,遇到这个问题,反复几次,也没有得到解决,而且进入了死循环,一直在提示这个消息,即使在cleanup之后. 百度了一番之后,找到了解决办法: 把出错的文件在本地删除掉, ...
- 首次用U盘安装CentOS
安之前看了这篇文章,http://www.osyunwei.com/archives/2307.html,然后就开始了. 首先下载ultraiso,官网下载的,将centos的iso镜像写到u盘上. ...
- Hyper-V 2012 R2 故障转移群集之建立域控(AD DS)与加入域
Windows 2012 R2建立域控(AD DS)与加入域 Active Directory概述: 使用 Active Directory(R) 域服务 (AD DS) 服务器角 ...
- RHEL6.5 换源
由于redhat的yum在线更新要收费,即没有注册的无法使用:将redhat的yum卸载.重装第三方源. 第三方源包括:网易,重庆大学,epel 等Author wood_man.2015.10.2 ...
- CentOS 系统目录解析
CentOS 系统目录解析 http://mp.weixin.qq.com/s?__biz=MzAxOTAzMzEzNg==&mid=202463614&idx=2&sn=51 ...