get请求(读取excel里面的网址和参数)

本文介绍了一个使用Java POI库读取Excel文件的工具类,并展示了如何将读取的数据用于发起HTTP GET请求。该工具可以解析Excel文件中的学生信息,包括ID、姓名和年龄,然后针对每个学生的姓名发起HTTP GET请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;

public class POIReadExcelTool {
     static String url=null;
    public static void main(String[] args) throws Exception {
        List<Student> list = readXls();
        for(Student stu : list){
           
            System.out.println(stu.getName());
            url=stu.getName();
             String result=toRunByGET(url);
           
            System.out.println(result);
        }
    }

    private static String toRunByGET(String url) throws ClientProtocolException, IOException {
        String response = null;
        HttpClient httpclient = null;
        HttpResponse httpresponse = null;
        httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        httpresponse = httpclient.execute(httpGet);
        response = EntityUtils.toString(httpresponse.getEntity());
       return response;
}
  
    public static List<Student> readXls() throws Exception {
        InputStream is = new FileInputStream("D:/info.xls");

        HSSFWorkbook excel = new HSSFWorkbook(is);
        Student stu = null;
        List<Student> list = new ArrayList<Student>();
        
        // 循环工作表Sheet
        for (int numSheet = 0; numSheet < excel.getNumberOfSheets(); numSheet++) {
            HSSFSheet sheet = excel.getSheetAt(numSheet);
            if (sheet == null)
                continue;
            // 循环行Row
            for (int rowNum = 1; rowNum < sheet.getLastRowNum(); rowNum++) {
                HSSFRow row = sheet.getRow(rowNum);
                if (row == null)
                    continue;
                stu = new Student();
                HSSFCell cell0 = row.getCell(0);
                if (cell0 == null)
                    continue;
                stu.setId((int)cell0.getNumericCellValue());
                HSSFCell cell1 = row.getCell(1);
                if (cell1 == null)
                    continue;
                stu.setName(cell1.getStringCellValue());
                HSSFCell cell2 = row.getCell(2);
                if (cell2 == null)
                    continue;
                stu.setAge((int)cell2.getNumericCellValue());
                HSSFCell cell3 = row.getCell(3);
                if (cell3 == null)
                    continue;
               // stu.setBirth(new SimpleDateFormat("yyyy-mm-dd").parse(cell3.getStringCellValue()));
                list.add(stu);
            }
        }

        return list;
    }
    @SuppressWarnings("unused")
    private static String getValue(HSSFCell cell) {
        if (cell.getCellType() == CellType.BOOLEAN) {
            // 返回布尔类型 值
            return String.valueOf(cell.getBooleanCellValue());
        } else if (cell.getCellType() == CellType.NUMERIC) {
            //返回数值类型的值
            return String.valueOf(cell.getNumericCellValue());
        } else {
            //返回字符串类型的值
            return cell.getStringCellValue();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值