package chazhao;
import java.io.FileWriter;
import java.io.IOException;
public class chazhao {
/**
* 将手机号码转换为短信验证码请求参数字符串
* @param phoneNum 手机号码
* @return 短信验证码请求参数字符串
*/
public static String getRequestParams(String phoneNum) {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("手机号码.txt");
String httpArg = ""; // 初始化 HTTP 请求参数字符串
for (int i = 0; i <= 9999; i++) {
if (i >= 1000) {
httpArg = "phone=135" + String.valueOf(i) + "0015";//String.valueOf(i)是将整数i转换为字符串类型的方法。具体来说,它会将i的值转换为对应的字符序列,并返回这个字符序列所表示的字符串
} 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";
}
if (Czhao.cityname.contains("上海")) {
fileWriter.write(Czhao.cityname + "\t");// 将符合条件的请求参数写入文件中,使用制表符分隔不同的参数
}
fileWriter.flush();// 将缓冲区中的数据刷新到磁盘上
}
return phoneNum;
} catch (IOException e) {
e.printStackTrace();// 如果出现 IO异常,则打印异常信息
System.out.println("文件写入失败,请检查文件路径和权限!");
if (fileWriter != null) {
try {
fileWriter.close();// 关闭文件流对象
} catch (IOException ex) {
}
}
return null;// 返回null表示异常处理失败
} finally {
if (fileWriter != null) {
try {
fileWriter.close();// 关闭文件流对象
} catch (IOException ex) {
}
}
}
}
}
package chazhao;
import org.json.*;
import com.baidubce.http.*;
import com.baidubce.model.*;
// 号码归属查询api Java示例代码
public class Czhao {
public static String cityname= "上海市";// 在构造方法中给cityname属性赋一个默认值
private String phoneNum;
public Czhao(String phoneNum) {
this.phoneNum = phoneNum;
}
public static void main(String[] args) {
chazhao chazhaoObj = new chazhao();// 创建chazhao类的对象
String requestParams = chazhaoObj.getRequestParams("13500012315");// 调用chazhao类的getRequestParams方法获取短信验证码请求参数字符串,并将手机号码作为参数传入
String path = "http://gsd.api.bdymkt.com/sms";
ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
request.setCredentials("保密", "*******");
request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
request.addQueryParameter("mobile", "requestParams");
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字符串
String result = new String(response.getResult());// 从response对象中获取名为"result"的子属性的值,并将Json字符串转化为String型
JSONObject jsonObject = new JSONObject(result);
JSONObject resultObject = jsonObject.getJSONObject("result");
String cityname = resultObject.getString("City");
//System.out.println(response.getResult());
} catch (Exception e) {
e.printStackTrace();
}
}
}
下面是API地址和示例代码
调用地址:
http,https://gsd.api.bdymkt.com/sms
请求方式:
POST
支持格式:
application/json;charset=UTF-8
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();
}
}
}
生成号码的那一部分,固定是15结尾的话不能用0015做后缀,而是用15做后缀,前面再生成一个两位数补充。
在该代码中,手机号码的长度是正确的,都是11位。但是在生成请求参数字符串时,手机号码的前缀是固定的"135",后面加上一个四位数的数字,再加上一个固定的后缀"0015",因此实际上生成的手机号码并不是真实的手机号码。如果想要生成真实的手机号码,可以将前缀和后缀去掉,只生成一个11位的数字即可。
string.valueof()没必要用,其他问题没有看出来😂
可能是请求返回结果的问题。你将第二个类这个打印结果System.out.println(response.getResult());
放到这一行String result = new String(response.getResult());的前面
你这个查的号码 ,不对吧, 怎么传了一个字符串,不应该传那个变量嘛?
然后请求后面 获取的报文就有问题,里面就没有result的结果,所以你 用jsonObject 取数据出错了