ITPub博客

首页 > 数字化转型 > ERP > 使用URL作为API,连接到对方并且提取返回信息

使用URL作为API,连接到对方并且提取返回信息

原创 ERP 作者:liang573728 时间:2019-03-27 11:00:06 0 删除 编辑
现在URL作为API的情况越来越多了,如:BlackBerry,F-Secure等提供客户的接口就是通过URL. 客户通过URL提出请求,处理后再通过页面返回处理结果。
作为处理着类case, Java是非常好的选择。
在工作中也常遇到这类case,现在就分享下代码。[@more@]import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.*;

public class Http_RepSMS {
private static String DEBUG;

public static void main (String args[]){
try{
if (args.length != 1){
System.out.println("Usage: java Http_ReqSMS ");
System.exit(1);

}

String url = args[0];
Http_RepSMS Run_Http_ReqSMS = new Http_RepSMS();
String re = Run_Http_ReqSMS.get_http(url);
//String re = "0[Send SMS success]";
//String re = "103[Incorrect password]";
System.out.println(re);
}
catch (Exception ex){
System.out.println("Error: "+ex);
}
}

public String get_http(String url) {
String tmpResult = null;
String result = null;
try {
URL urlObj = new URL(url);
URLConnection URLConnectionObj = urlObj.openConnection();
BufferedReader dataStream = new BufferedReader ( new InputStreamReader(URLConnectionObj.getInputStream()));
while ( (tmpResult = dataStream.readLine()) != null){
result = result + tmpResult;
}
}
catch (Exception ex){
System.out.println("Error: "+ex);
result = "Error: "+ex;
}
return result;
}
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/640706/viewspace-1055908/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2005-10-15

  • 博文量
    152
  • 访问量
    138341