我错误的原因是想把原来的for循环用于现在的API中,但是原来的API只有一个apikey并且是get请求,而现在的API有apikey和secretkey并且是POST请求
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();
}
}
}