HTTP请求范例
packagecom.grefr.basemethod;
/*JAVA发送HTTP请求,返回HTTP响应内容,实例及应用博客分类:JAVA实现
Java.netBeanJDKApache.
JDK中提供了一些对无状态协议请求(HTTP)的支持,下面我就将我所写的一个小例子(组件)进行描述:
首先让我们先构建一个请求类(HttpRequester)。
该类封装了JAVA实现简单请求的代码,如下:*/
//Java代码
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.net.HttpURLConnection;
importjava.net.URL;
importjava.nio.charset.Charset;
importjava.util.Map;
importjava.util.Vector;
/**
*HTTP请求对象
*
*@authorYYmmiinngg
*/
publicclassHttpRequester{
privateStringdefaultContentEncoding;
publicHttpRequester(){
this.defaultContentEncoding=Charset.defaultCharset().name();
}
/**
*发送GET请求
*
*@paramurlString
*URL地址
*@return响应对象
*@throwsIOException
*/
publicHttpResponssendGet(StringurlString)throwsIOException{
returnthis.send(urlString,"GET",null,null);
}
/**
*发送GET请求
*
*@paramurlString
*URL地址
*@paramparams
*参数集合
*@return响应对象
*@throwsIOException
*/
publicHttpResponssendGet(StringurlString,Map<String,String>params)
throwsIOException{
returnthis.send(urlString,"GET",params,null);
}
/**
*发送GET请求
*
*@paramurlString
*URL地址
*@paramparams
*参数集合
*@parampropertys
*请求属性
*@return响应对象
*@throwsIOException
*/
publicHttpResponssendGet(StringurlString,Map<String,String>params,
Map<String,String>propertys)throwsIOException{
returnthis.send(urlString,"GET",params,propertys);
}
/**
*发送POST请求
*
*@paramurlString
*URL地址
*@return响应对象
*@throwsIOException
*/
publicHttpResponssendPost(StringurlString)throwsIOException{
returnthis.send(urlString,"POST",null,null);
}
/**
*发送POST请求
*
*@paramurlString
*URL地址
*@paramparams
*参数集合
*@return响应对象
*@throwsIOException
*/
publicHttpResponssendPost(StringurlString,Map<String,String>params)
throwsIOException{
returnthis.send(urlString,"POST",params,null);
}
/**
*发送POST请求
*
*@paramurlString
*URL地址
*@paramparams
*参数集合
*@parampropertys
*请求属性
*@return响应对象
*@throwsIOException
*/
publicHttpResponssendPost(StringurlString,Map<String,String>params,
Map<String,String>propertys)throwsIOException{
returnthis.send(urlString,"POST",params,propertys);
}
/**
*发送HTTP请求
*
*@paramurlString
*@return响映对象
*@throwsIOException
*/
privateHttpResponssend(StringurlString,Stringmethod,
Map<String,String>parameters,Map<String,String>propertys)
throwsIOException{
HttpURLConnectionurlConnection=null;
if(method.equalsIgnoreCase("GET")&¶meters!=null){
StringBufferparam=newStringBuffer();
inti=0;
for(Stringkey:parameters.keySet()){
if(i==0)
param.append("?");
else
param.append("&");
param.append(key).append("=").append(parameters.get(key));
i++;
}
urlString+=param;
}
URLurl=newURL(urlString);
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(method);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
if(propertys!=null)
for(Stringkey:propertys.keySet()){
urlConnection.addRequestProperty(key,propertys.get(key));
}
if(method.equalsIgnoreCase("POST")&¶meters!=null){
StringBufferparam=newStringBuffer();
for(Stringkey:parameters.keySet()){
param.append("&");
param.append(key).append("=").append(parameters.get(key));
}
urlConnection.getOutputStream().write(param.toString().getBytes());
urlConnection.getOutputStream().flush();
urlConnection.getOutputStream().close();
}
returnthis.makeContent(urlString,urlConnection);
}
/**
*得到响应对象
*
*@paramurlConnection
*@return响应对象
*@throwsIOException
*/
privateHttpResponsmakeContent(StringurlString,
HttpURLConnectionurlConnection)throwsIOException{
HttpResponshttpResponser=newHttpRespons();
try{
InputStreamin=urlConnection.getInputStream();
BufferedReaderbufferedReader=newBufferedReader(
newInputStreamReader(in));
httpResponser.contentCollection=newVector<String>();
StringBuffertemp=newStringBuffer();
Stringline=bufferedReader.readLine();
while(line!=null){
httpResponser.contentCollection.add(line);
temp.append(line).append("\r\n");
line=bufferedReader.readLine();
}
bufferedReader.close();
Stringecod=urlConnection.getContentEncoding();
if(ecod==null)
ecod=this.defaultContentEncoding;
httpResponser.urlString=urlString;
httpResponser.defaultPort=urlConnection.getURL().getDefaultPort();
httpResponser.file=urlConnection.getURL().getFile();
httpResponser.host=urlConnection.getURL().getHost();
httpResponser.path=urlConnection.getURL().getPath();
httpResponser.port=urlConnection.getURL().getPort();
httpResponser.protocol=urlConnection.getURL().getProtocol();
httpResponser.query=urlConnection.getURL().getQuery();
httpResponser.ref=urlConnection.getURL().getRef();
httpResponser.userInfo=urlConnection.getURL().getUserInfo();
httpResponser.content=newString(temp.toString().getBytes(),ecod);
httpResponser.contentEncoding=ecod;
httpResponser.code=urlConnection.getResponseCode();
httpResponser.message=urlConnection.getResponseMessage();
httpResponser.contentType=urlConnection.getContentType();
httpResponser.method=urlConnection.getRequestMethod();
httpResponser.connectTimeout=urlConnection.getConnectTimeout();
httpResponser.readTimeout=urlConnection.getReadTimeout();
returnhttpResponser;
}catch(IOExceptione){
throwe;
}finally{
if(urlConnection!=null)
urlConnection.disconnect();
}
}
/**
*默认的响应字符集
*/
publicStringgetDefaultContentEncoding(){
returnthis.defaultContentEncoding;
}
/**
*设置默认的响应字符集
*/
publicvoidsetDefaultContentEncoding(StringdefaultContentEncoding){
this.defaultContentEncoding=defaultContentEncoding;
}
}
/*其次我们来看看响应对象(HttpRespons)。响应对象其实只是一个数据BEAN,由此来封装请求响应的结果数据,如下:
java代码*/
importjava.util.Vector;
/**
*响应对象
*/
publicclassHttpRespons{
StringurlString;
intdefaultPort;
Stringfile;
Stringhost;
Stringpath;
intport;
Stringprotocol;
Stringquery;
Stringref;
StringuserInfo;
StringcontentEncoding;
Stringcontent;
StringcontentType;
intcode;
Stringmessage;
Stringmethod;
intconnectTimeout;
intreadTimeout;
Vector<String>contentCollection;
publicStringgetContent(){
returncontent;
}
publicStringgetContentType(){
returncontentType;
}
publicintgetCode(){
returncode;
}
publicStringgetMessage(){
returnmessage;
}
publicVector<String>getContentCollection(){
returncontentCollection;
}
publicStringgetContentEncoding(){
returncontentEncoding;
}
publicStringgetMethod(){
returnmethod;
}
publicintgetConnectTimeout(){
returnconnectTimeout;
}
publicintgetReadTimeout(){
returnreadTimeout;
}
publicStringgetUrlString(){
returnurlString;
}
publicintgetDefaultPort(){
returndefaultPort;
}
publicStringgetFile(){
returnfile;
}
publicStringgetHost(){
returnhost;
}
publicStringgetPath(){
returnpath;
}
publicintgetPort(){
returnport;
}
publicStringgetProtocol(){
returnprotocol;
}
publicStringgetQuery(){
returnquery;
}
publicStringgetRef(){
returnref;
}
publicStringgetUserInfo(){
returnuserInfo;
}
}
importjava.util.Vector;
*//**
*响应对象
*//*
publicclassHttpRespons{
StringurlString;
intdefaultPort;
Stringfile;
Stringhost;
Stringpath;
intport;
Stringprotocol;
Stringquery;
Stringref;
StringuserInfo;
StringcontentEncoding;
Stringcontent;
StringcontentType;
intcode;
Stringmessage;
Stringmethod;
intconnectTimeout;
intreadTimeout;
Vector<String>contentCollection;
publicStringgetContent(){
returncontent;
}
publicStringgetContentType(){
returncontentType;
}
publicintgetCode(){
returncode;
}
publicStringgetMessage(){
returnmessage;
}
publicVector<String>getContentCollection(){
returncontentCollection;
}
publicStringgetContentEncoding(){
returncontentEncoding;
}
publicStringgetMethod(){
returnmethod;
}
publicintgetConnectTimeout(){
returnconnectTimeout;
}
publicintgetReadTimeout(){
returnreadTimeout;
}
publicStringgetUrlString(){
returnurlString;
}
publicintgetDefaultPort(){
returndefaultPort;
}
publicStringgetFile(){
returnfile;
}
publicStringgetHost(){
returnhost;
}
publicStringgetPath(){
returnpath;
}
publicintgetPort(){
returnport;
}
publicStringgetProtocol(){
returnprotocol;
}
publicStringgetQuery(){
returnquery;
}
publicStringgetRef(){
returnref;
}
publicStringgetUserInfo(){
returnuserInfo;
}
}
最后,让我们写一个应用类,测试以上代码是否正确
Java代码
importcom.yao.http.HttpRequester;
importcom.yao.http.HttpRespons;
publicclassTest{
publicstaticvoidmain(String[]args){
try{
HttpRequesterrequest=newHttpRequester();
HttpResponshr=request.sendGet("http://www.csdn.net");
System.out.println(hr.getUrlString());
System.out.println(hr.getProtocol());
System.out.println(hr.getHost());
System.out.println(hr.getPort());
System.out.println(hr.getContentEncoding());
System.out.println(hr.getMethod());
System.out.println(hr.getContent());
}catch(Exceptione){
e.printStackTrace();
}
}
}
相关推荐
iviewer是一个具有缩放和图像旋转功能的图像查看小部件。在jQuery官网下载后,有很多文件。直接把文件夹解压拖到项目里。然后再html中引入主要的文件。-- iviewer-鼠标滚轮js -->