- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.URL;
- import java.net.URLConnection;
- public class TestPost {
- public static void testPost() throws IOException {
- /**
- * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using
- * java.net.URL and //java.net.URLConnection
- *
- * 使用页面发送请求的正常流程:在页面http://www.faircanton.com/message/loginlytebox.asp中输入用户名和密码,然后按登录,
- * 跳转到页面http://www.faircanton.com/message/check.asp进行验证
- * 验证的的结果返回到另一个页面
- *
- * 使用java程序发送请求的流程:使用URLConnection向http://www.faircanton.com/message/check.asp发送请求
- * 并传递两个参数:用户名和密码
- * 然后用程序获取验证结果
- */
- URL url = new URL("http://www.faircanton.com/message/check.asp");
- URLConnection connection = url.openConnection();
- /**
- * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。
- * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做:
- */
- connection.setDoOutput(true);
- /**
- * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ...
- */
- OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "8859_1");
- out.write("username=kevin&password=*********"); //向页面传递数据。post的关键所在!
- // remember to clean up
- out.flush();
- out.close();
- /**
- * 这样就可以发送一个看起来象这样的POST:
- * POST /jobsearch/jobsearch.cgi HTTP 1.0 ACCEPT:
- * text/plain Content-type: application/x-www-form-urlencoded
- * Content-length: 99 username=bob password=someword
- */
- // 一旦发送成功,用以下方法就可以得到服务器的回应:
- String sCurrentLine;
- String sTotalString;
- sCurrentLine = "";
- sTotalString = "";
- InputStream l_urlStream;
- l_urlStream = connection.getInputStream();
- // 传说中的三层包装阿!
- BufferedReader l_reader = new BufferedReader(new InputStreamReader(
- l_urlStream));
- while ((sCurrentLine = l_reader.readLine()) != null) {
- sTotalString += sCurrentLine + "/r/n";
- }
- System.out.println(sTotalString);
- }
- public static void main(String[] args) throws IOException {
- testPost();
- }
- }
这个程序我已经测试通过的。
如果除了post一些数据外,还要上传文件,可以使用下面两个函数之一:
- /**
- * 通过HTTP协议向指定的网络地址发送文件。
- * @param params 传输过程中需要传送的参数
- * @param filename 需要传送的文件在本地的位置。
- * @throws TransferException
- */
- public String doPost(HashMap params, InputStream stream)
- throws TransferException
- {
- URLConnection conn = null; // URL连结对象。
- BufferedReader in = null; // 请求后的返回信息的读取对象。
- String keyName = null;
- try
- {
- conn = url.openConnection();
- conn.setUseCaches(false);
- conn.setDoOutput(true);
- conn.setRequestProperty("Content-Type", "multipart/form-data");
- // 设置参数
- if (params != null)
- {
- Set keys = params.keySet();
- // 遍历参数集取得参数名称和值
- if (!keys.isEmpty())
- {
- Iterator iterator = keys.iterator();
- while (iterator.hasNext())
- {
- keyName = (String) iterator.next();
- // 将参数加入到连接对象中
- conn.addRequestProperty(
- keyName,
- (String) params.get(keyName));
- }
- }
- }
- // 构造传输文件
- //FileInputStream fis = new FileInputStream(filename);
- BufferedInputStream bis = new BufferedInputStream( stream );
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int ch;
- while ((ch = bis.read()) != -1)
- baos.write(ch);
- byte[] fileData = baos.toByteArray();
- // 传输文件。
- DataOutputStream dos =
- new DataOutputStream(
- new BufferedOutputStream(conn.getOutputStream()));
- dos.write(fileData);
- dos.flush();
- dos.close();
- in =
- new BufferedReader(
- new InputStreamReader(conn.getInputStream()));
- //in.close();
- }
- catch (FileNotFoundException fe)
- {
- InputStream err = ((HttpURLConnection) conn).getErrorStream();
- if (err == null)
- throw new TransferException("网络传输时发生的未知错误");
- in = new BufferedReader(new InputStreamReader(err));
- }
- catch (IOException ioe)
- {
- ioe.printStackTrace();
- throw new TransferException("网络传输错误!");
- }
- // 返回提示信息
- StringBuffer response = new StringBuffer();
- String line;
- try
- {
- while ((line = in.readLine()) != null)
- response.append(line + "/n");
- in.close();
- }
- catch (IOException ioe)
- {
- ioe.getStackTrace();
- throw new TransferException("网络响应错误!");
- }
- return response.toString();
- }
- /**
- * 通过HTTP协议向指定的网络地址发送文件。
- * @param params 传输过程中需要传送的参数
- * @param data 需要传送的内容。
- * @throws TransferException
- */
- public InputStream doPost(HashMap params, byte[] data)
- throws TransferException
- {
- URLConnection conn = null; // URL连结对象。
- BufferedReader in = null; // 请求后的返回信息的读取对象。
- String keyName = null;
- try
- {
- conn = url.openConnection();
- conn.setUseCaches(false);
- conn.setDoOutput(true);
- conn.setRequestProperty("Content-Type", "multipart/form-data");
- // 设置参数
- if (params != null)
- {
- Set keys = params.keySet();
- // 遍历参数集取得参数名称和值
- if (!keys.isEmpty())
- {
- Iterator iterator = keys.iterator();
- while (iterator.hasNext())
- {
- keyName = (String) iterator.next();
- // 将参数加入到连接对象中
- conn.addRequestProperty(
- keyName,
- (String) params.get(keyName));
- }
- }
- }
- // 传输文件。
- DataOutputStream dos =
- new DataOutputStream(
- new BufferedOutputStream(conn.getOutputStream()));
- dos.write(data);
- dos.flush();
- dos.close();
- return conn.getInputStream();
- }
- catch (FileNotFoundException fe)
- {
- InputStream err = ((HttpURLConnection) conn).getErrorStream();
- if (err == null)
- throw new TransferException("网络传输时发生的未知错误");
- else
- throw new TransferException("未知错误");
- }
- catch (IOException ioe)
- {
- ioe.printStackTrace();
- throw new TransferException("网络传输错误!");
- }
- }
这两个函数是公司的员工写的,还没有用实际的例子测试过。
我们还可以使用htmlparse的jar包(该包及相关文档可以在 http://htmlparser.sourceforge.net/这里下载)提供的函数对获取的html进行解析
例子如下:
- import org.htmlparser.Node;
- import org.htmlparser.NodeFilter;
- import org.htmlparser.Parser;
- import org.htmlparser.filters.TagNameFilter;
- import org.htmlparser.tags.TableTag;
- import org.htmlparser.util.NodeList;
- public class TestHTMLParser {
- public static void testHtml() {
- try {
- String sCurrentLine;
- String sTotalString;
- sCurrentLine = "";
- sTotalString = "";
- java.io.InputStream l_urlStream;
- java.net.URL l_url = new java.net.URL("http://www.ideagrace.com/html/doc/2006/07/04/00929.html");
- java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
- l_connection.connect();
- l_urlStream = l_connection.getInputStream();
- java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream));
- while ((sCurrentLine = l_reader.readLine()) != null) {
- sTotalString += sCurrentLine+"/r/n";
- // System.out.println(sTotalString);
- }
- String testText = extractText(sTotalString);
- System.out.println( testText );
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static String extractText(String inputHtml) throws Exception {
- StringBuffer text = new StringBuffer();
- Parser parser = Parser.createParser(new String(inputHtml.getBytes(),"GBK"), "GBK");
- // 遍历所有的节点
- NodeList nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
- public boolean accept(Node node) {
- return true;
- }
- });
- System.out.println(nodes.size()); //打印节点的数量
- for (int i=0;i<nodes.size();i++){
- Node nodet = nodes.elementAt(i);
- //System.out.println(nodet.getText());
- text.append(new String(nodet.toPlainTextString().getBytes("GBK"))+"/r/n");
- }
- return text.toString();
- }
- public static void test5(String resource) throws Exception {
- Parser myParser = new Parser(resource);
- myParser.setEncoding("GBK");
- String filterStr = "table";
- NodeFilter filter = new TagNameFilter(filterStr);
- NodeList nodeList = myParser.extractAllNodesThatMatch(filter);
- TableTag tabletag = (TableTag) nodeList.elementAt(11);
- }
- public static void main(String[] args) throws Exception {
- // test5("http://www.ggdig.com");
- testHtml();
- }
- }
Java实现发送http请求<-------------method two--------->
JDK中提供了一些对无状态协议请求(HTTP)的支持:
首先让我们先构建一个请求类(HttpRequester)。
该类封装了JAVA实现简单请求的代码,如下:
<==========================================================================HttpRequester ========================================>
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
*/
public class HttpRequester {
private String defaultContentEncoding;
public HttpRequester() {
this.defaultContentEncoding = Charset.defaultCharset().name();
}
/**
* 发送GET请求
* 发送GET请求
*
* @param urlString
* URL地址
* @param params
* 参数集合*
* @param urlString
* URL地址
* @return 响应对象
* @throws IOException
*/
public HttpRespons sendGet(String urlString) throws IOException {
return this.send(urlString, "GET", null, null);
}
/**
* @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;
}
}
<=============================================================================HttpRequester =============================================>
其次我们来看看响应对象(HttpRespons)。
响应对象其实只是一个数据BEAN,由此来封装请求响应的结果数据,如下:
<=============================================================================HttpRespons =============================================>
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;
}
}
<=============================================================================HttpRespons =============================================>
<=============================================================================class test=============================================>
最后,让我们写一个应用类,测试以上代码是否正确
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();
}
}
}
<=============================================================================class test=============================================>