1 .获取请求
publicstaticstringdoget (string httpurl,String contentType ) {
//content type=' application/JSON ';
contentType='text/xml ';
httpurlconnectionconnection=null;
InputStream is=null;
缓冲读取br=null;
string result=空; //返回结果字符串
try {
//创建远程url连接对象
URLURL=newURL(httpurl;
//在远程url连接对象中打开单个连接,并将其强烈转换为httpURLConnection类
connection=(httpurlconnection ) url.openConnection );
//设置连接方式: get
connection.setrequestmethod (' get );
//设置与主机服务器的连接超时时间: 15000毫秒
connection.setconnecttimeout (15000;
//设置读取远程返回的数据的时间: 60000毫秒
connection.setreadtimeout (60000;
connection.setrequestproperty (' content-type ',contentType );
connection.setrequestproperty (' content-length ','0' );
//发送请求
connection.connect (;
通过connection连接获取输入流
连接. getresponse code (==200 ) {
is=connection.getInputStream (;
//封装输入流is并指定字符集
br=newbufferedreader (newinputstreamreader (is,' UTF-8 ' ) );
//保存数据
string buffers BF=new string buffer (;
string temp=空;
while((temp=br.readline ) )!=null ) {
SBF.append(temp;
SBF.append('rn ' );
}
result=sbf.toString (;
}
}catch(malformedurlexceptione ) )
e .打印任务跟踪(;
}catch(ioexceptione ) {
e .打印任务跟踪(;
} finally {
//关闭资源
if (空!=br
try {
br.close (;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}
}
if (空!=is({
try {
is.close (;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}
}
connection.disconnect (; //关闭远程连接
}
返回结果;
}
2.POST请求转发对象
由第一约翰处理
在pom.xml中引入fastjson依赖
com.alibaba
fastjson
1.2.70
publicstaticstringdopost (string httpurl,对象参数)。
httpurlconnectionconnection=null;
InputStream is=null;
输出流输出=空;
缓冲读取br=null;
string result=空;
try {
URLURL=newURL(httpurl;
//通过远程url连接对象打开连接
connection=(httpurlconnection ) url.openConnection );
//设置连接请求方式
connection.setrequestmethod (' post );
//设置连接的主机服务器的超时时间: 15000毫秒
connection.setconnecttimeout (15000;
//设置读取主机服务器返回数据的超时时间: 60000毫秒
connection.setreadtimeout (60000;
//默认值为: false,在将数据传输/写入远程服务器时必须设置为true
connection.setdooutput(true;
//默认值为: true,并且当当前向远程服务读取数据时设置为true,则此参数可以具有或不具有
connection.setdoinput(true;
//传递的参数格式:请求参数必须为name1=value1name2=value2。
connection.setrequestproperty (' content-type ',' application/json ' );
//凭据设置:授权: bearer da3e fcbf-0845-4fe3-8ABA-ee 040 be 542 c 0
//connection.setrequestproperty (authorization,) bearer da3e fcbf-0845-4fe3-8ABA-ee 040 be 542 c0 ) );
//从连接目标获取输出流
out=connection.getOutputStream (;
//通过输出流对象导出/传输参数。 这是通过字节数组写入的
//OS.write(param.getbytes ) );
//JSONObject obj=new JSONObject (;
//java.net.URLEncoder.encode编码后有%多余%的问题,无法在传输过程中解析
//string JSON=Java.net.urlencoder.encode (obj.tostring ),' utf-8 ' );
objecto=JSON object.to JSON (param );
objectjsonobject=JSON object.parse (o.tostring () );
out.write(JSONobject.tostring ().getBytes ) );
//从连接目标获取输入流并远程读取
连接. getresponse code (==200 ) {
is=connection.getInputStream (;
//包装输入流对象的:charset根据工作项目组的要求进行设置
br=newbufferedreader (newinputstreamreader (is,' UTF-8 ' ) );
string buffers BF=new string buffer (;
string temp=空;
//逐行循环读取数据
while((temp=br.readline ) )!=null ) {
SBF.append(temp;
SBF.append('rn ' );
}
result=sbf.toString (;
}
}catch(malformedurlexceptione ) )
e .打印任务跟踪(;
}catch(ioexceptione ) {
e .打印任务跟踪(;
} finally {
//关闭资源
if (空!=br
try {
br.close (;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}
}
if (空!=out
try {
out.close (;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}
}
if (空!=is({
try {
is.close (;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}
}
//断开与远程地址url的连接
connection.disconnect (;
}
返回结果;
}
3 .在传递get请求时可能需要对参数进行编码
url=url '? msgtype code=' urlencoder.encode (msgtype code ) (usercode='urlencoder.encode ) usercode ) ) current=' current ' si