JAVA发送HTTP请求,返回HTTP响应内容,应用及实例代码
程序员文章站
2024-02-21 23:59:10
jdk 中提供了一些对无状态协议请求(http )的支持,下面我就将我所写的一个小例子(组件)进行描述:首先让我们先构建一个请求类(httprequester )。该类封装...
jdk 中提供了一些对无状态协议请求(http )的支持,下面我就将我所写的一个小例子(组件)进行描述:
首先让我们先构建一个请求类(httprequester )。
该类封装了 java 实现简单请求的代码,如下:
复制代码 代码如下:
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.net.httpurlconnection;
import java.net.url;
import java.nio.charset.charset;
import java.util.map;
import java.util.vector;
/**
* http请求对象
*
* @author yymmiinngg
*/
public class httprequester {
private string defaultcontentencoding;
public httprequester() {
this.defaultcontentencoding = charset.defaultcharset().name();
}
/**
* 发送get请求
*
* @param urlstring
* url地址
* @return 响应对象
* @throws ioexception
*/
public httprespons sendget(string urlstring) throws ioexception {
return this.send(urlstring, "get", null, null);
}
/**
* 发送get请求
*
* @param urlstring
* url地址
* @param params
* 参数集合
* @return 响应对象
* @throws ioexception
*/
public httprespons sendget(string urlstring, map<string, string> params)
throws ioexception {
return this.send(urlstring, "get", params, null);
}
/**
* 发送get请求
*
* @param urlstring
* url地址
* @param params
* 参数集合
* @param propertys
* 请求属性
* @return 响应对象
* @throws ioexception
*/
public httprespons sendget(string urlstring, map<string, string> params,
map<string, string> propertys) throws ioexception {
return this.send(urlstring, "get", params, propertys);
}
/**
* 发送post请求
*
* @param urlstring
* url地址
* @return 响应对象
* @throws ioexception
*/
public httprespons sendpost(string urlstring) throws ioexception {
return this.send(urlstring, "post", null, null);
}
/**
* 发送post请求
*
* @param urlstring
* url地址
* @param params
* 参数集合
* @return 响应对象
* @throws ioexception
*/
public httprespons sendpost(string urlstring, map<string, string> params)
throws ioexception {
return this.send(urlstring, "post", params, null);
}
/**
* 发送post请求
*
* @param urlstring
* url地址
* @param params
* 参数集合
* @param propertys
* 请求属性
* @return 响应对象
* @throws ioexception
*/
public httprespons sendpost(string urlstring, map<string, string> params,
map<string, string> propertys) throws ioexception {
return this.send(urlstring, "post", params, propertys);
}
/**
* 发送http请求
*
* @param urlstring
* @return 响映对象
* @throws ioexception
*/
private httprespons send(string urlstring, string method,
map<string, string> parameters, map<string, string> propertys)
throws ioexception {
httpurlconnection urlconnection = null;
if (method.equalsignorecase("get") && parameters != null) {
stringbuffer param = new stringbuffer();
int i = 0;
for (string key : parameters.keyset()) {
if (i == 0)
param.append("?");
else
param.append("&");
param.append(key).append("=").append(parameters.get(key));
i++;
}
urlstring += param;
}
url url = new url(urlstring);
urlconnection = (httpurlconnection) url.openconnection();
urlconnection.setrequestmethod(method);
urlconnection.setdooutput(true);
urlconnection.setdoinput(true);
urlconnection.setusecaches(false);
if (propertys != null)
for (string key : propertys.keyset()) {
urlconnection.addrequestproperty(key, propertys.get(key));
}
if (method.equalsignorecase("post") && parameters != null) {
stringbuffer param = new stringbuffer();
for (string key : parameters.keyset()) {
param.append("&");
param.append(key).append("=").append(parameters.get(key));
}
urlconnection.getoutputstream().write(param.tostring().getbytes());
urlconnection.getoutputstream().flush();
urlconnection.getoutputstream().close();
}
return this.makecontent(urlstring, urlconnection);
}
/**
* 得到响应对象
*
* @param urlconnection
* @return 响应对象
* @throws ioexception
*/
private httprespons makecontent(string urlstring,
httpurlconnection urlconnection) throws ioexception {
httprespons httpresponser = new httprespons();
try {
inputstream in = urlconnection.getinputstream();
bufferedreader bufferedreader = new bufferedreader(
new inputstreamreader(in));
httpresponser.contentcollection = new vector<string>();
stringbuffer temp = new stringbuffer();
string line = bufferedreader.readline();
while (line != null) {
httpresponser.contentcollection.add(line);
temp.append(line).append("\r\n");
line = bufferedreader.readline();
}
bufferedreader.close();
string ecod = 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 = new string(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();
return httpresponser;
} catch (ioexception e) {
throw e;
} finally {
if (urlconnection != null)
urlconnection.disconnect();
}
}
/**
* 默认的响应字符集
*/
public string getdefaultcontentencoding() {
return this.defaultcontentencoding;
}
/**
* 设置默认的响应字符集
*/
public void setdefaultcontentencoding(string defaultcontentencoding) {
this.defaultcontentencoding = defaultcontentencoding;
}
}
其次我们来看看响应对象(httprespons )。 响应对象其实只是一个数据bean ,由此来封装请求响应的结果数据,如下:
复制代码 代码如下:
import java.util.vector;
/**
* 响应对象
*/
public class httprespons {
string urlstring;
int defaultport;
string file;
string host;
string path;
int port;
string protocol;
string query;
string ref;
string userinfo;
string contentencoding;
string content;
string contenttype;
int code;
string message;
string method;
int connecttimeout;
int readtimeout;
vector<string> contentcollection;
public string getcontent() {
return content;
}
public string getcontenttype() {
return contenttype;
}
public int getcode() {
return code;
}
public string getmessage() {
return message;
}
public vector<string> getcontentcollection() {
return contentcollection;
}
public string getcontentencoding() {
return contentencoding;
}
public string getmethod() {
return method;
}
public int getconnecttimeout() {
return connecttimeout;
}
public int getreadtimeout() {
return readtimeout;
}
public string geturlstring() {
return urlstring;
}
public int getdefaultport() {
return defaultport;
}
public string getfile() {
return file;
}
public string gethost() {
return host;
}
public string getpath() {
return path;
}
public int getport() {
return port;
}
public string getprotocol() {
return protocol;
}
public string getquery() {
return query;
}
public string getref() {
return ref;
}
public string getuserinfo() {
return userinfo;
}
}
最后,让我们写一个应用类,测试以上代码是否正确
复制代码 代码如下:
import com.yao.http.httprequester;
import com.yao.http.httprespons;
public class test {
public static void main(string[] args) {
try {
httprequester request = new httprequester();
httprespons hr = request.sendget("//www.jb51.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 (exception e) {
e.printstacktrace();
}
}
}