Java语言后台实现调用url路径并传参

来源:互联网 发布:java中int的范围 编辑:程序博客网 时间:2024/06/08 14:41

**首先:需要俩jar包,可以在我个人资源库里下载

package test;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class InvokingUrl {
public static void main(String[] args) throws ClientProtocolException, IOException {
url();
}
/**
* [数据连接url]<br>
* @auther lixingshuai<br>
* @2016-9-6  上午11:49:42<br>
* @throws ClientProtocolException
* @throws IOException<br>
*/
@SuppressWarnings("deprecation")
public static boolean url() throws ClientProtocolException, IOException{
// 创建HttpClient实例     
        HttpClient httpclient = new DefaultHttpClient();
        //http://192.168.66.156:8080/calf/data?p=fun&m=synDataFromPlatform&ip=192.168.1.234&machineName=test&type=3&role=8&isEmail=1&mStatus=1&isBusiness=1&region=0
        String params ="http://192.168.66.156:8080/calf/data?p=fun&m=synDataFromPlatform&ip=192.168.1.234&machineName=test&type=3&role=8&isEmail=1&mStatus=1&isBusiness=1&region=0";
        //System.out.println(params);
        // 创建Get方法实例     
        HttpGet httpgets = new HttpGet(params);    
        HttpResponse response = httpclient.execute(httpgets);    
        HttpEntity entity = response.getEntity();    
        if (entity != null) {    
            InputStream instreams = entity.getContent();    
            String str = convertStreamToString(instreams);  
            System.out.println(str);
            // Do not need the rest    
            httpgets.abort();
            return true;
        }else{
        return false;
        }
}
public static  String convertStreamToString(InputStream is) {      
       BufferedReader reader = new BufferedReader(new InputStreamReader(is));      
       StringBuilder sb = new StringBuilder();      
       String line = null;      
       try {      
           while ((line = reader.readLine()) != null) {  
               sb.append(line + "\n");      
           }      
       } catch (IOException e) {      
           e.printStackTrace();      
       } finally {      
           try {      
               is.close();      
           } catch (IOException e) {      
              e.printStackTrace();      
           }      
       }      
       return sb.toString();      
  } 
}
0 0
原创粉丝点击