- 浏览: 56940 次
- 性别:
- 来自: 武汉
文章分类
最新评论
本文转自:http://yymmiinngg.iteye.com/blog/154258. 灰常感谢!
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; } }
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("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(); } } }
发表评论
-
日常积累
2013-09-18 16:41 413eclispe的tomcat设置 -Xms128m -X ... -
对象修改日志
2012-05-11 11:15 2131转自:http://hi.baidu.com/41 ... -
spring bean 获取 ServletContext
2012-04-12 10:23 1223我的需求是获取spring开发的web项目在服务器上的绝对路径 ... -
jdk1.6生成webservice的 p命令
2012-03-12 09:26 648wsimport -keep -p service.clien ... -
Java 1.5新特性Enum的学习和使用
2012-03-02 17:23 1067很多时候我们定义了一 ... -
axis2 axis2-1.6.1 客户端 adb方式调用下最少jar包
2011-12-23 16:55 1041太痛恨自己了,maven没有学好就是这样,不然的话,很快搞定. ... -
银行货币正则表达式
2011-11-24 16:49 1683/** * * @Title: isBankF ... -
去掉.svn文件
2011-10-11 16:41 801双击这个reg文件。成功后,在每一个文件夹上点击右键都会 ... -
Visual Swing 安装到myeclipse中
2011-05-27 19:57 1871在eclipse 目录下建立两个文件夹,一个MyPlugins ...
相关推荐
该程序输入URL发送HTTP请求,获得返回的响应后,将网络页面保存在当地主机上。如果第一层页面包含相同hostname的链接,则同时下载该链接。
java代码-使用java解决http请求SOAP webService接口的源代码 ——学习参考资料:仅用于个人学习使用!
这个文档是java调用天气的方法,希望对你有所帮助
(method 表示请求方式, url 请求路径, asynch 是否异步,对于 asynch 的值通常不会设置, 默认是true,表示异步发送请求) send(content) :向服务器发送请求 属性: onreadystatechange :状态回调函数 ...
2、在LR中选择java协议的工程,然后再action部分调用如下; import postAndGet_result.java 事务开始 public static void main(String[] args) { //发送 GET 请求 String s=postAndGet_result.sendGet(...
代码使用Java内置的HttpServer类创建一个HTTP服务器,并将处理程序映射到"/callback"上下文路径。在处理程序中,我们从HTTP请求中获取查询字符串,并将其附加到响应消息中。然后,我们发送一个200响应代码和响应长度...
HTTP 接口调用跟调用本地方法一样自然优雅将 HTTP 请求和接口绑定,然后由框架生成接口的代理类,直接调用接口的方法就会自动构建请求参数并发送请求,然后处理请求响应转换为接口方法的返回值返回(支持泛型)。...
NULL 博文链接:https://kisseveryone.iteye.com/blog/1728187
分析了下程序,写了个测试,本地跑依旧长时间等待,无法运行,也不抛出异常,最后发现问题在于发送请求的部分,一直在等待对方服务器响应,由于采用的是keep-alive方式的连接,这里没有采用超时的方式,以至于程序...
在这个过程中,Java 程序需要使用到 SOAP 协议来发送请求和接收响应,实现对 Webservice 的调用。 首先,需要了解 Webservice 的基本概念。Webservice 是一种基于网络的软件系统,可以提供各种服务,如天气预报、...
Servlet 是一种基于 Java 语言的 Web 组件,它可以处理 HTTP 请求,并返回响应结果。Servlet 与 CGI 的主要区别在于,Servlet 是基于 Java 语言的,而 CGI 是基于 C 语言的。 1.6. Servlet 的主要任务 Servlet 的...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
3. Web Service 请求和响应可以在不同的应用程序之间发送和接收,包括不同业务的计算机。 Web Service 通常由三个基本组件组成: 1. 查找和注册服务的机制。 2. 服务的输入和输出参数的定义。 3. 访问服务的传输...
演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口函数得到远程...
JSP部署于网络服务器上,可以响应客户端发送的请求,并根据请求内容动态地生成HTML、XML或其他格式文档的Web网页,然后返回给请求者。JSP技术以Java语言作为脚本语言,为用户的HTTP请求提供服务,并能与服务器上的...
Logbook是一个可扩展的Java库,用于为不同的客户端和服务器端技术启用完整的请求和响应日志记录。 它满足了以下特殊需求:a)允许Web应用程序开发人员记录应用程序接收或发送的所有HTTP通信b)以易于持久化并在以后...
使用Java进行网络请求,获取天气API的响应数据。 解析JSON格式的响应数据,提取所需的天气信息。 构建一个图形用户界面(GUI),让用户输入城市名称或坐标。 显示实时天气信息,包括温度、天气状况、湿度和风速等。 ...
演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口函数得到远程...
演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口函数得到远程...
演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口函数得到远程...