package com.cmy.urlcon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; public class HttpRequestor { private String charset = "utf-8";
private Integer connectTimeout = null;
private Integer socketTimeout = null;
private String proxyHost = null;
private Integer proxyPort = null; public String doGet(String urlPath) throws Exception { URL url = new URL(urlPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded"); InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bfReader = null;
StringBuffer sb = new StringBuffer();
String readLine = null; if (connection.getResponseCode() >= 300) {
throw new Exception("HTTP Request is failue, Response code is "
+ connection.getResponseCode());
} try {
inputStream = connection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
bfReader = new BufferedReader(inputStreamReader);
while ((readLine = bfReader.readLine()) != null) {
sb.append(readLine);
} } catch (Exception e) {
e.printStackTrace();
} finally {
bfReader.close();
inputStreamReader.close();
inputStream.close();
} return sb.toString(); } public static String doPost(String urlPath, Map paramMap) throws Exception {
URL url = new URL(urlPath);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuffer paramBuffer = new StringBuffer(); if (paramMap != null) {
Iterator iterator = paramMap.keySet().iterator();
String key = null;
String value = null;
while (iterator.hasNext()) {
key = (String) iterator.next(); if ((value = (String) paramMap.get(key)) != null) {
paramBuffer.append(value);
} else {
value = "";
}
paramBuffer.append(key).append("=").append(value);
if (iterator.hasNext()) {
paramBuffer.append("&");
}
}
}
System.out.println("POST parameter: " + paramBuffer.toString()); con.setRequestProperty("Accept-Charset", "utf-8");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setConnectTimeout(3000);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
String cookie0 = con.getHeaderField("Set-Cookie"); OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bfReader = null;
StringBuffer sb = new StringBuffer();
String readLine = null; try {
outputStream = con.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(paramBuffer.toString());
outputStreamWriter.flush(); } catch (Exception e) {
e.printStackTrace();
} if (con.getResponseCode() >= 300) {
throw new Exception("HTTP Request is failue, Response code is "
+ con.getResponseCode());
} try {
inputStream = con.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
bfReader = new BufferedReader(inputStreamReader);
while ((readLine = bfReader.readLine()) != null) {
sb.append(readLine);
} } catch (Exception e) {
e.printStackTrace();
} finally {
outputStreamWriter.close();
outputStream.close();
bfReader.close();
inputStreamReader.close();
inputStream.close();
} return sb.toString();
} private HttpURLConnection openConnection(URL localURL) throws IOException {
HttpURLConnection connection;
if (proxyHost != null && proxyPort != null) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
connection = (HttpURLConnection) localURL.openConnection(proxy);
} else {
connection = (HttpURLConnection) localURL.openConnection();
}
return connection;
} private void renderRequest(HttpURLConnection connection) { if (connectTimeout != null) {
connection.setConnectTimeout(connectTimeout);
} if (socketTimeout != null) {
connection.setReadTimeout(socketTimeout);
} } public String getCharset() {
return charset;
} public void setCharset(String charset) {
this.charset = charset;
} public Integer getConnectTimeout() {
return connectTimeout;
} public void setConnectTimeout(Integer connectTimeout) {
this.connectTimeout = connectTimeout;
} public Integer getSocketTimeout() {
return socketTimeout;
} public void setSocketTimeout(Integer socketTimeout) {
this.socketTimeout = socketTimeout;
} public String getProxyHost() {
return proxyHost;
} public void setProxyHost(String proxyHost) {
this.proxyHost = proxyHost;
} public Integer getProxyPort() {
return proxyPort;
} public void setProxyPort(Integer proxyPort) {
this.proxyPort = proxyPort;
} /**
* test case
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
/* Post Request */
Map dataMap = new HashMap();
dataMap.put("username", "Nick Huang");
dataMap.put("blog", "IT");
System.out.println(new HttpRequestor().doPost("http://localhost:8080/OneHttpServer/", dataMap)); /* Get Request */
System.out.println(new HttpRequestor().doGet("http://localhost:8080/OneHttpServer/")); } }

http://www.runoob.com/java/java-url-processing.html

最新文章

  1. 初学者--bootstrap(六)组件中的下拉菜单----在路上(10)
  2. HTML 常用标签
  3. worldwind一些资料
  4. 《JavaScript高级程序设计》读书笔记--前言
  5. 【BZOJ-3786】星系探索 Splay + DFS序
  6. java servlet的工作原理
  7. 图解 SQL 各种连接查询之间的区别
  8. B - Fill
  9. poj 3981 字符串替换
  10. P - 奔小康赚大钱 - hdu 2255(带权值的匹配)
  11. SQL学习之去重复查询
  12. CentOS下自动登陆root帐户
  13. SQL执行WebService
  14. Intel_AV-ICE06加速卡+QAT_Engine测试
  15. Bootstrap table方法,Bootstrap table事件,配置
  16. RBM:深度学习之Restricted Boltzmann Machine的BRBM学习+LR分类—Jason niu
  17. 20135327郭皓--Linux内核分析第九周 期中总结
  18. A Spy in the Metro(UVA 1025 ACM/ICPC World Finals2003)
  19. mosquitto 配置文件解说
  20. JS使用中碰到的一些问题

热门文章

  1. 杂项-TOOL:NPIO
  2. HTML5: input:file上传类型控制
  3. autocomplete 自动填充 combobox
  4. Web 单元测试
  5. unity3d之Editor的Assembly-CSharp.dll文件路径
  6. IntelliJ IDEA创建maven web项目(IDEA新手适用)
  7. @Slf4j注解
  8. 深入浅出理解依赖注入这种由外部负责其依赖需求的行为,我们可以称其为 “控制反转(IoC)”
  9. delphi android路径 TPath 文件路径,文件管理
  10. Java编程最差实践