电音少女 2023-05-28 19:14 采纳率: 46.2%
浏览 186
已结题

谁能告诉我错在哪里并加以改正?

我错误的原因是想把原来的for循环用于现在的API中,但是原来的API只有一个apikey并且是get请求,而现在的API有apikey和secretkey并且是POST请求

img

img


package chazhao;
import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import java.io.*;
import java.net.*;
public class chazhao {
    public static void main(String[] args) throws IOException  {
        FileWriter fileWriter = new FileWriter("手机号码.txt");
        String httpUrl = "http://gsd.api.bdymkt.com/sms";
        String httpArg = "";
        for (int i = 0; i < 9999; i++) {
            if (i >= 1000) {
                httpArg = "phone=135" + String.valueOf(i) + "0015";
            } else if (i >= 100) {
                httpArg = "phone=1350" + String.valueOf(i) + "0015";
            } else if (i >= 10) {
                httpArg = "phone=13500" + String.valueOf(i) + "0015";
            } else {
                httpArg = "phone=135000" + String.valueOf(i) + "0015";
            }
            String jsonResult = request(httpUrl, httpArg);
            if (jsonResult.contains("上海")) {
                fileWriter.write(httpArg + "\n\t");
            }
        }
        fileWriter.flush();
        fileWriter.close();
    }

    /**
     * @param urlAll  :请求接口
     * @param httpArg :参数
     * @return 返回结果
     */
    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sb = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();//打开url连接
            connection.setConnectTimeout(15000);//设置连接超时时间
            connection.setReadTimeout(15000);//设置读取超时时间
            connection.setRequestMethod("POST");//设置请求参数,即具体的http方法
            // 填入apikey到HTTP header
            connection.setRequestProperty("apikey", " *************");
            connection.setDoOutput(true);//设置是否向httpUrlConnection输出;对于post请求,参数要放在http正问内,因此需要设置为true(默认情况下是false)
            connection.setDoInput(true);//设置是否从httpUrlConnection读入,默认情况下是true
            connection.connect();//调用connect连接远程资源
            InputStream is = connection.getInputStream();//利用getInputStream()访问资源数据
            BufferedReader reader1 = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader1.readLine()) != null) {
                sb.append(strRead);
                sb.append("\r\n");
            }
            reader.close();
            result = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}


下面是API示例代码


import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;

// 号码归属查询api Java示例代码
public class RequestDemo {
    public static void main(String[] args) {
        String path = "http://gsd.api.bdymkt.com/sms";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        request.setCredentials("您的 access key", "您的 secret key");

        request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
        
        request.addQueryParameter("mobile", "");
        
        String requestExample = "\r\nimport com.baidubce.http.ApiExplorerClient;\r\nimport com.baidubce.http.AppSigner;\r\nimport com.baidubce.http.HttpMethodName;\r\nimport com.baidubce.model.ApiExplorerRequest;\r\nimport com.baidubce.model.ApiExplorerResponse;\r\n\r\n\/\/ 号码归属查询api Java示例代码\r\npublic class RequestDemo {\r\n    public static void main(String[] args) {\r\n        String path = \"http:\/\/gsd.api.bdymkt.comhttp:\/\/gwgp-g8eennmvmcz.n.bdcloudapi.com\/sms\";\r\n        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);\r\n        request.setCredentials(\"您的 access key\", \"您的 secret key\");\r\n\r\n        request.addHeaderParameter(\"Content-Type\", \"application\/json;charset=UTF-8\");\r\n        \r\n        request.addQueryParameter(\"mobile\", \"\");\r\n        \r\n        \r\n\r\n        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());\r\n\r\n        try {\r\n          ApiExplorerResponse response = client.sendRequest(request);\r\n          \/\/ 返回结果格式为Json字符串\r\n          System.out.println(response.getResult());\r\n        } catch (Exception e) {\r\n          e.printStackTrace();\r\n        }\r\n    }\r\n}";
        request.setJsonBody(requestExample);
        

        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

        try {
          ApiExplorerResponse response = client.sendRequest(request);
          // 返回结果格式为Json字符串
          System.out.println(response.getResult());
        } catch (Exception e) {
          e.printStackTrace();
        }
    }
}

  • 写回答

5条回答 默认 最新

  • Soulic 2023-05-28 19:47
    关注

    那个sms的http接口写错了吧,request异常,返回的result为null,所以调contains方法的时候报NPE

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月2日
  • 修改了问题 5月30日
  • 修改了问题 5月29日
  • 修改了问题 5月29日
  • 展开全部

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址