从接口接入数据

scala读取接口数据,一般的接口

import java.net.{HttpURLConnection, URL}
import org.apache.commons.io.IOUtils
object HttpsRequest{
def getHttpsRequest(itemid: String): String = {  
   //传入需要获取的URL地址和参数,itemid 是需要传入的参数,new一个URL
  val restServiceURL = new URL(s"""http://localhost:45678/ITEM?itemid=$itemid""")   
  //获取连接实例
  val httpURLConnection = restServiceURL.openConnection.asInstanceOf[HttpURLConnection]     //一般scala 使用asinstanceOf来实现实例化
  httpURLConnection.setRequestMethod("GET")   //配置连接模式
  httpURLConnection.setConnectTimeout(6000000)   //连接超时设置
  httpURLConnection.setReadTimeout(6000000)   //读取数据超时设置
  httpURLConnection.setRequestProperty("Content-Type", "application/json")  //设置请求类型属性 
  httpURLConnection.setDoOutput(true)   
  if (httpURLConnection.getResponseCode != 200) {  //判断是否连上了数据
  " "   
  } else {     
  val result = IOUtils.toString(httpURLConnection.getInputStream, "UTF-8")    //输出连接数据,以及设置输出数据的编码格式
  result    //返回结果 
        }
    }
  }

二、java 连接base64 加密的接口数据

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;import java.net.URLConnection;
import java.util.HashMap;import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.tomcat.util.codec.binary.Base64;
               public class HttpRequest {
			   /** 向指定 URL 发送POST方法的请求	 
			   * @param fullurl  完整的URL,可能包括一些参数如 	
			   * @param user     用户	
			   * @param password 密码	 
			   * @return 所代表远程资源的响应结果	 
			   */	
			       public static String sendGet(String fullurl, String user, String password) {	
				       StringBuffer result = new StringBuffer();
				       BufferedReader in = null;
				   try {			
				     String urlNameString = fullurl;	
				     URL realUrl = new URL( urlNameString );	
				     // 打开和URL之间的连接			
				      URLConnection connection = realUrl.openConnection();
				     // 设置通用的请求属性
				     //connection.setRequestProperty("accept", "*/*");
				     //connection.setRequestProperty("connection", "Keep-Alive");	
				     //connection.setRequestProperty("user-agent",			
					 //	"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
					 String authString = user + ":" + password;			
					 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
					 String authStringEnc = new String(authEncBytes);			
                               //connection.setRequestProperty( "Authorization", "Basic base64() " );			                  
                                 connection.setRequestProperty("Authorization", "Basic "+authStringEnc);
					 // 建立实际的连接	
					 connection.connect();			
					 // 获取所有响应头字段			
					 Map > map = connection.getHeaderFields();
                                // 遍历所有的响应头字段
                                			//for (String key : map.keySet()) {
                                						//	System.out.println(key + "--->" + map.get(key));	
                                								//}			
                                								// 定义 BufferedReader输入流来读取URL的响应	
                         in = new BufferedReader( new InputStreamReader(	 connection.getInputStream() ) );
                               													
String line;			
while ((line = in.readLine()) != null) {				
result.append( line );			}		
} catch (Exception e) {	
		System.err.println( "发送GET请求出现异常!" + e );	
				e.printStackTrace();	
					}	
					// 使用finally块来关闭输入流	
						finally {			
						try {				
						if (in != null) {					
						in.close();				
						}			
						}catch (Exception e2) {	
						 			e2.printStackTrace();
						 						}	
						 				}
						 					return result.toString();
						 						}

你可能感兴趣的:(scala)