- 浏览: 164417 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
GreatExpectations:
666可以可以哦
js并行加载,顺序执行 -
yiway:
如果是跨域的话,window.parent是拒绝访问的(由于w ...
利用HTML5的window.postMessage实现跨域通信 -
yiway:
如果是跨域的话,window.parent是决绝访问的(由于w ...
利用HTML5的window.postMessage实现跨域通信
package com.grefr.basemethod;
/*JAVA发送HTTP请求,返回HTTP响应内容,实例及应用 博客分类: JAVA实现
Java.netBeanJDKApache .
JDK 中提供了一些对无状态协议请求(HTTP )的支持,下面我就将我所写的一个小例子(组件)进行描述:
首先让我们先构建一个请求类(HttpRequester )。
该类封装了 JAVA 实现简单请求的代码,如下: */
//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 ,由此来封装请求响应的结果数据,如下:
java代码 */
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 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;
}
}
最后,让我们写一个应用类,测试以上代码是否正确
Java代码
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( "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 (Exception e) {
e.printStackTrace();
}
}
}
/*JAVA发送HTTP请求,返回HTTP响应内容,实例及应用 博客分类: JAVA实现
Java.netBeanJDKApache .
JDK 中提供了一些对无状态协议请求(HTTP )的支持,下面我就将我所写的一个小例子(组件)进行描述:
首先让我们先构建一个请求类(HttpRequester )。
该类封装了 JAVA 实现简单请求的代码,如下: */
//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 ,由此来封装请求响应的结果数据,如下:
java代码 */
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 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;
}
}
最后,让我们写一个应用类,测试以上代码是否正确
Java代码
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( "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 (Exception e) {
e.printStackTrace();
}
}
}
发表评论
-
RMI,RPC,SOAP对比分析
2014-01-02 11:29 1486详见: http://blog.yemou.net/art ... -
Http和Socket详解
2014-01-02 11:22 1487详见: http://blog.yem ... -
RMI和socket详解
2014-01-02 11:21 1188详见: http://blog.yemou.net/art ... -
FTP的主动和被动模式详解
2013-05-24 19:55 1925详见:http://blog.yemou.net/articl ... -
HTTP协议是无状态协议,怎么理解
2013-05-24 19:54 965详见:http://blog.yemou.net/ ... -
http长连接与短连接
2013-05-24 19:53 2298详见:http://blog.yemou.net/ ... -
HTTP协议Keep-Alive模式详解
2013-05-24 19:52 1046详见:http://blog.yemou.net/articl ... -
HTTP中Get与Post的区别
2013-05-21 14:57 867详见:http://blog.yemou.net/ ...
相关推荐
HTTP协议工作过程 HTTP请求范例: POST /servlet/default.JSP HTTP/1.1 Accept: text/plain; text/...
HTTP 的工作方式是客户机与服务器之间的请求-应答协议。 web 浏览器可能是客户端,而计算机上的网络应用程序也可能作为服务器端。 举例:客户端(浏览器)向服务器提交 HTTP 请求;服务器向客户端返回响应。响应...
这是我做的一个关于项目管理的程序的Android端,采用抽屉侧边栏(drawerlayout)布局,采用handler机制切换主页面ListVIew的数据,采用http+json获取数据。其中数据统计引入了Android的图表插件。
React提取 一个很小的React组件来获取HTTP请求。 它要求React> = 16.3.0入门$ # With npm$ npm install --save react-fetchino$ # Or yarn$ yarn add react-fetchino道具名称类型必需的描述网址细绳真的要获取的资源...
:dog_face: React钩子发出同构的HTTP请求 npm i 产品特点 SSR(服务器端渲染)支持 TypeScript支持 2个依赖项( , ) GraphQL支持(查询+突变) 提供者来设置默认的url和options 请求/响应拦截器 React本机...
JavaScript中使用Json,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/46741577
通过在iOS , macOS和Linux上声明Web服务和终结点,以清晰,安全的方式发出HTTP请求 在使用Swift进行URL请求时,主要有两个选择:在Foundation中使用URLSession API或使用一些繁琐的框架。 该框架设计为轻量级,...
Inspector是一个简单的HTTP服务器,它仅使用JSON对传入的请求进行编码,并将其写为响应,从而使检查标头变得容易。 范例回应 { " Method " : " GET " , " URL " : { " Scheme " : " " , " Opaque " : " " , " ...
这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本...
看代码就会的小例子:php合并js请求复制代码 代码如下://页面保存为js.php//前台请求范例http://localhost/js.php?f=1,2//请求1.js,2.js两个文件<?PHPheader(“Content-Type:application/x-javascript”);header...
范例影片: : JavaScript请求编码数据 < script src =" ajaxCrypto.js " > </ script > < script > ajaxCrypto ( "post-or-get" , "test.php" , { isim : "ihaci" , soyisim : "berkpw" } , ...
lib curl 实现http client示例
DeTor :speak-no-evil_monkey: 一个简单的REST API,用于识别从TOR网络发出的请求。 DeTor在内部使用 ,而不是TOR出口节点的静态列表。...通话范例 简单的一个 只需打开在您的浏览器,并期待在found领域,它
记录该请求 通过记录器请求记录。 包含ZLogging功能,该功能提供了请求记录程序手段,其中包含用于处理...设定范例import { httpListener } from '@hatsy/hatsy' ;import { ZLogging } from '@hatsy/log-z-request' ;im
WCF_REST_HTTP方式GET和POST,可以通过网站直接请求数据,http协议,不需要部署iis,直接运行程序就可以通过网站访问到数据 教程地址 http://www.cnblogs.com/artech/archive/2012/02/04/wcf-rest-sample.html
Servlet通过servlet引擎运行在Web服务器中,以执行请求和响应,请求、响应的典型范例是HTTP协议。 一个客户端程序,可以是一个Web浏览器,或者是非其他的可以连接上Internet的程序,它会访问Web服务器并发出请求。这...
全书内容丰富,主要包括减少HTTP请求、EdgeComputing技术、ExpiresHeader技术、Gzip组件、CSS和JavaScript最佳实践、主页内联、Domain最小化、JavaScript优化、避免重定向的技巧、删除重复JavaScript的技巧、关闭...
介绍如何通过HttpClient模块来创建Http连接,并分别以Http GET与Http POST方法来传递参数,连接之后取回Web ...重点是如何使用HttpClient的模块来完成Http的请求与应答。 分享参考自Android SDK开发范例大全第3版。
全书内容丰富,主要包括减少HTTP请求、EdgeComputing技术、ExpiresHeader技术、Gzip组件、CSS和JavaScript最佳实践、主页内联、Domain最小化、JavaScript优化、避免重定向的技巧、删除重复JavaScript的技巧、关闭...
概述验证基本身份验证-例如安全的Traefik仪表板仪表盘将Traefik信息中心公开到子域记录中启用记录启用访问日志 港口 附加入口点范例程式码 服务非HTTP / HTTPS端口范例程式码 重新导向 HTTP到HTTPS重定向 全局HTTP到...