精华内容
下载资源
问答
  • JAVA访问URL

    千次阅读 2014-11-02 00:15:28
    JAVA访问URL: package Test; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLC

    JAVA访问URL:


    package Test;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.awt.Desktop;
    
    public class URLTest {
    
        public static void main(String[] args) {
    		String urlStr = "http://www.baidu.com";  
    		URL url;
    		try {
    		    url = new URL(urlStr);
    		    URLConnection URLconnection = url.openConnection();  
    		    HttpURLConnection httpConnection = (HttpURLConnection)URLconnection;  
    		    int responseCode = httpConnection.getResponseCode();  
    		    if (responseCode == HttpURLConnection.HTTP_OK) {  
    		        System.err.println("成功");
    		        InputStream urlStream = httpConnection.getInputStream();  
    		        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlStream));  
    		        String sCurrentLine = "";  
    		        String sTotalString = "";  
    		        while ((sCurrentLine = bufferedReader.readLine()) != null) {  
    		            sTotalString += sCurrentLine;  
    		        }  
    		        System.err.println(sTotalString); 	
    		        runBroswer(urlStr);
    		    }else{
    		        System.err.println("失败");
    		     }
    		} catch (Exception e) {
    		    e.printStackTrace();
    		}  
                  
        }
        
        public static void runBroswer(String webSite) {
            try {
                Desktop desktop = Desktop.getDesktop();
                if (desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
                    URI uri = new URI(webSite);
                    desktop.browse(uri);
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (URISyntaxException ex) {
                ex.printStackTrace();
            }
        }
    }
    





    展开全文
  • java访问url

    2014-05-27 13:49:25
    import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;... * java访问url  *  */ public class JavaA
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    /**
     * java访问url
     *
     */
    public class JavaAccessUrl {
        public static String getUrlContent(String str){
            BufferedReader read = null;
            InputStream input = null;
            StringBuffer sb = new StringBuffer();
            String line = "";
            
            try {
                URL url = new URL(str);
                HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

                httpConn.setConnectTimeout(5000);//设置连接服务器的超时时间
                httpConn.setReadTimeout(3000);//设置从服务器读取数据的超时时间
                httpConn.connect();
                input = httpConn.getInputStream();
                read = new BufferedReader(new InputStreamReader(input, "utf-8"));
                while ((line = read.readLine()) != null) {
                    sb.append(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            String result = sb.toString();
            return result;
        }
    }
    展开全文
  • java访问url_JAVA访问URL

    2021-02-12 09:40:19
    JAVA访问URL:package Test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URI;...

    JAVA访问URL:

    package Test;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.InputStreamReader;

    import java.net.HttpURLConnection;

    import java.net.URI;

    import java.net.URISyntaxException;

    import java.net.URL;

    import java.net.URLConnection;

    import java.awt.Desktop;

    public class URLTest {

    public static void main(String[] args) {

    String urlStr = "http://www.baidu.com";

    URL url;

    try {

    url = new URL(urlStr);

    URLConnection URLconnection = url.openConnection();

    HttpURLConnection httpConnection = (HttpURLConnection)URLconnection;

    int responseCode = httpConnection.getResponseCode();

    if (responseCode == HttpURLConnection.HTTP_OK) {

    System.err.println("成功");

    InputStream urlStream = httpConnection.getInputStream();

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlStream));

    String sCurrentLine = "";

    String sTotalString = "";

    while ((sCurrentLine = bufferedReader.readLine()) != null) {

    sTotalString += sCurrentLine;

    }

    System.err.println(sTotalString);

    runBroswer(urlStr);

    }else{

    System.err.println("失败");

    }

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    public static void runBroswer(String webSite) {

    try {

    Desktop desktop = Desktop.getDesktop();

    if (desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {

    URI uri = new URI(webSite);

    desktop.browse(uri);

    }

    } catch (IOException ex) {

    ex.printStackTrace();

    } catch (URISyntaxException ex) {

    ex.printStackTrace();

    }

    }

    }

    展开全文
  • java访问URL

    2017-09-21 09:11:37
    //访问URLpublic class HttpUtil { public static String callWebPage(String urlStr, String postData) { return callWebPage(urlStr, postData, "UTF-8"); } public static String callWebPage(String ...
    //访问URL
    public class HttpUtil {
    	public static String callWebPage(String urlStr, String postData) {
    		return callWebPage(urlStr, postData, "UTF-8");
    	}
    	
    	public static String callWebPage(String urlStr, String postData, String encoding) {
    
    		String rStr = null;
    		BufferedReader in = null;
    		PrintWriter out = null;
    		try {
    			// 打开和URL之间的连接
    			URLConnection conn = (new URL(urlStr)).openConnection();
    			StringBuffer sb = new StringBuffer();
    			// 设置通用的请求属性
    			conn.setRequestProperty("accept", "*/*");
    			conn.setRequestProperty("connection", "Keep-Alive");
    			conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    			if (postData != null) {
    				conn.setDoOutput(true);
    				conn.setDoInput(true);
    			}
    			// 建立实际的连接
    			conn.connect();
    			// Post数据(如果非空的话, 获取URLConnection对象对应的输出流并输出参数)
    			if (postData != null) {
    				if(encoding==null || "".equals(encoding)){
    					out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()), true);
    				}else{
    					out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),  encoding), true);
    				}
    				out.print(postData);
    				out.flush();
    			}
    			// 获取所有响应头字段
    			// Map<String, List<String>> map = connection.getHeaderFields();
    			// for (String key : map.keySet()) { System.out.println(key +
    			// " <== " + map.get(key)); }
    			// 定义 BufferedReader输入流来读取URL的响应
    			if(encoding==null || "".equals(encoding)){
    				in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    			}else{
    				in = new BufferedReader(new InputStreamReader(conn.getInputStream(),  encoding));
    			}
    			String line;
    			while ((line = in.readLine()) != null) {
    				sb.append(line);
    			}
    			rStr = sb.toString();
    		} catch (Exception e) {
    			LogUtil.exception(e);
    		} finally {
    			try {
    				if (out != null) {
    					out.close();
    				}
    			} catch (Exception ee) {
    			}
    			try {
    				if (in != null) {
    					in.close();
    				}
    			} catch (Exception ee) {
    			}
    		}
    		return rStr;
    	}
    
    }


    
    
    展开全文
  • Java访问url

    千次阅读 2017-09-26 10:04:11
    try { URL url = new URL("http://baiduu.com"); InputStream in =url.openStream(); InputStreamReader isr = new InputStreamReader(in); BufferedReade
  • java 访问url

    千次阅读 2015-06-29 11:07:07
    在平时开发中,我们一般都需要实现外部的url,贴上本人开发使用的三种访问代码。希望点评 第一种: public class HttpUtils { public static void main(String[] args) { System.out.println(SMS("", ...
  • import java.io.*;import java.net.URL;/** * java访问URL并下载文件 * @author yangjuqi 2007-12-14 下午04:08:51 * */public class ResourceURLServer{public static void getURLResource(St...
  • import java.io.*;import java.net.HttpURLConnection;.../*** java访问URL并下载文件* @author yangjuqi 2007-12-14 下午04:08:51**/public class nn1 {public static void saveToFile(String destUrl, Str...
  • 使用URL类访问url,传递参数,完成操作。Java代码 StringurlStr="";URLurl=newURL(urlStr);URLConnectionURLconnection=url.openConnection();HttpURLConnectionhttpConnection=(HttpURLConnection)URLconnection;...
  • java访问 url_java调用url

    2021-02-12 13:13:50
    importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io....importjava.net.URL;importjava.net.URLConnection;importjava.util.List;importjava.util.Ma...
  • JAVA访问url工具类

    2019-12-04 08:56:14
    JAVA访问url工具类(包含post和get方式请求) package org.hyena.hr.analysisReport.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io....
  • 最近在做接口,做自己的,也要把别人的接口封装到自己的接口里,比如直接访问url就能获取json数据的一种接口调用方式(我自己的也是这样对外提供的,感觉比较懒省事)。不仅自己封装别人的需要先读取到数据,在自己...
  • /*** 程序中访问http数据接口*/public static String getURLContent(String urlStr) {/** 网络的url地址 */URL url = null;/** http连接 */HttpURLConnection httpConn = null;/**//** 输入流 */BufferedReader in =...
  • [Java教程]java根据url获取json对象02013-12-27 18:00:05package test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io....
  • /*** 程序中访问http数据接口*/public static String getURLContent(String urlStr) {/** 网络的url地址 */URL url = null;/** http连接 */HttpURLConnection httpConn = null;/**//** 输入流 */BufferedReader in =...
  • java访问URL并下载文件

    2014-07-08 16:46:02
    import java.io.*; import java.net.HttpURLConnection; import java.net.URL;... * java访问URL并下载文件 * @author yangjuqi 2007-12-14 下午04:08:51 * */ public class nn1 { public static void...
  • 需求:testCase 访问本地的 url,获得返回 url 的 json 数据1,url 路径 比如访问 http://localhost:8080/spb/resultController如下@RestController@RequestMapping("/spb")public class JsonController extends ...
  • java访问url编码

    2015-10-16 13:56:47
    使用java程序或者服务器后端程序访问某个url时,可以使用java.net.URL类,也可以使用apache的HttpClient包。 为了验证功能,在本地程序中可简单使用URL类,代码如下: [code="java"] public static ...
  • 这可能是一个愚蠢的问题,但是javaURL读取json的最简单的方式是什么?在Groovy中,这只是几行代码的问题。 我发现的Java示例代码长(并有巨大的异常处理块)。我想做的只是阅读这个链接的内容。回答:使用Maven工件...
  • import java.io.*; import java.net.URL;.../** * java访问URL并下载文件 * @author yangjuqi 2007-12-14 下午04:08:51 * */ public class ResourceURLServer {  public static void get...
  • JAVA访问url获取结果

    2018-01-05 14:47:34
    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection;
  • 我以最简单的方式完成了json解析器,在这里package ... import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Unsupp...
  • 使用Maven工件org.json:json我得到了下面的代码,我认为是很短的。...import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.i...
  • JAVA访问url POST请求

    千次阅读 2015-06-13 12:25:35
    分享一把~~~返回页面import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.security.KeyStore; import java.util.ArrayList; ...
  • Java访问URL实现接口测试

    千次阅读 2019-03-28 10:58:13
    httpClient httpConnection httpUnit
  • java 访问url接口问题

    2016-08-23 17:25:54
    URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect();为什么我直接这么写,发送不过去 URL url = new URL(urlString); HttpURLConnection ...
  • import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;...import java.net.MalformedURLException;...import java.net.URL; impor...
  • java访问url的问题

    2015-06-16 15:44:37
    [img=https://img-bbs.csdn.net/upload/201506/16/1434440663_533902.png][/img] url没错,在浏览器上可以访问 , 这个为什么不能访问
  • publicstaticString loadJSON (String url) { StringBuilder json =newStringBuilder(); try { URL oracle =newURL(url); URLConnection yc = oracle.openConnection(); BufferedReader in =newBufferedReader...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 7,493
精华内容 2,997
关键字:

java访问url

java 订阅