访问 java_Java 访问网页

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLConnection;

import java.net.URLEncoder;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

/**

* @author GaoFeng Chen

*/

public class AllResult {

/**

* @param args

* @throws Exception

*/

public static void main(String[] args) throws Exception {

// 要访问的网页

String urlStr = "http://xxxxxx";

URL url;

// 读文件,访问网页的引数放在一个文件中

List seqList = readFile("D:\\", "seq.txt");

if (seqList.size() > 0) {

// 每次运行时将前一次的结果清空

write("D:\\seqResult.txt", "OVERWRITE", new StringBuffer(

"the name of the sequence").append(" ")

.append("the result").toString());

int i = 1;

for (String seq : seqList) {

System.out.println("The current row number is: " + i++);

try {

// 准备引数

Map parameters = setParameter(seq);

// 获取引数

String parameter = getParameter(parameters);

byte[] b = parameter.getBytes();

url = new URL(urlStr);

URLConnection URLconnection = url.openConnection();

HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;

httpConnection.setDoOutput(true);

// Read from the connection. Default is true.

httpConnection.setDoInput(true);

// Set the post method. Default is GET

httpConnection.setRequestMethod("POST");

// Post cannot use caches

httpConnection.setUseCaches(false);

OutputStream reqOut = null;

reqOut = httpConnection.getOutputStream();

reqOut.write(b, 0, b.length);

int responseCode = httpConnection.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {

InputStream urlStream = httpConnection.getInputStream();

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(urlStream));

String sCurrentLine = "";

while ((sCurrentLine = bufferedReader.readLine()) != null) {

// 只把自己感兴趣的返回信息取出来

if (sCurrentLine.contains("XXXXX")) {

// 将获取的数据写入到指定的文件

write("D:\\seqResult.txt", "APPEND",

new StringBuffer(seq).append(":")

.append(sCurrentLine)

.toString());

}

}

} else {

System.err.println("FAIL");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

System.out.println("All of the sequence is over");

}

/**

*

* @param sequence

* @return

*/

public static Map setParameter(String sequence) {

Map parameters = new HashMap();

parameters.put("xxxx", "xx");

parameters.put("xxxx", sequence);

parameters.put("xxxx", "xx");

parameters.put("xxxx", "xx");

parameters.put("xxxx", "xx");

parameters.put("xxxx", "xx");

return parameters;

}

/**

*

* @param parameters

* @return

* @throws UnsupportedEncodingException

*/

public static String getParameter(Map parameters)

throws UnsupportedEncodingException {

StringBuffer parameter = new StringBuffer();

for (java.util.Iterator iter = parameters.entrySet().iterator(); iter

.hasNext();) {

Entry element = (Entry) iter.next();

parameter.append(element.getKey().toString());

parameter.append("=");

parameter.append(URLEncoder.encode(element.getValue().toString(),

"UTF-8"));

parameter.append("&");

}

if (parameter.length() > 0) {

parameter = parameter.deleteCharAt(parameter.length() - 1);

}

return parameter.toString();

}

/**

* reader the file

*

* @param dataPath

* path

* @param fileName

* fileName

* @return data file

*/

public static List readFile(String dataPath, String fileName) {

File file = new File(dataPath + fileName);

BufferedReader reader = null;

List value = new ArrayList();

try {

reader = new BufferedReader(new FileReader(file));

String tempString = null;

while ((tempString = reader.readLine()) != null) {

value.add(tempString);

}

reader.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

return value;

}

/**

*

* @param strFileName

* @param strWriteMode

* @param lstOutputData

* @return

* @throws Exception

*/

public static int write(String strFileName, String strWriteMode,

String outputData) throws Exception {

FileOutputStream fileOutputStream;

OutputStreamWriter outputStreamWriter;

BufferedWriter bufferedWriter;

int countOutput;

fileOutputStream = null;

outputStreamWriter = null;

bufferedWriter = null;

countOutput = 0;

try {

if (strFileName == null)

throw new IllegalArgumentException();

if (strWriteMode == null)

throw new IllegalArgumentException();

if (!"OVERWRITE".equals(strWriteMode)

&& !"APPEND".equals(strWriteMode))

throw new IllegalArgumentException();

if (outputData == null)

throw new IllegalArgumentException();

File file = new File(strFileName);

if ("OVERWRITE".equals(strWriteMode))

fileOutputStream = new FileOutputStream(file);

else

fileOutputStream = new FileOutputStream(file, true);

outputStreamWriter = new OutputStreamWriter(fileOutputStream,

"MS932");

bufferedWriter = new BufferedWriter(outputStreamWriter);

// for (int i = 0; i < lstOutputData.size(); i++) {

// String strLine = (String) lstOutputData.get(i);

bufferedWriter.write(outputData);

bufferedWriter.write("\n");

countOutput++;

// }

} catch (Exception ex) {

}

closeOutputStream(fileOutputStream, outputStreamWriter, bufferedWriter);

return countOutput;

}

/**

*

* @param fileOutputStream

* @param outputStreamWriter

* @param bufferedWriter

*/

private static void closeOutputStream(FileOutputStream fileOutputStream,

OutputStreamWriter outputStreamWriter, BufferedWriter bufferedWriter) {

if (bufferedWriter != null)

try {

bufferedWriter.close();

} catch (IOException ex) {

}

if (outputStreamWriter != null)

try {

outputStreamWriter.close();

} catch (IOException ex) {

}

if (fileOutputStream != null)

try {

fileOutputStream.close();

} catch (IOException ex) {

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值