获取URL相关信息

import java.net.URL;
import java.net.URLConnection;
import java.net.ConnectException;
import java.io.IOException;
import java.util.Date;
import java.net.HttpURLConnection;

public class GetURLInfo{
    public static void printInfo(URL url){
        System.out.println("==================URL info==================");
        //URL基本信息
        System.out.println("File ["+url.getFile()+"]");
        System.out.println("Protocol ["+url.getProtocol()+"]");
        System.out.println("Authority ["+url.getAuthority()+"]");
        System.out.println("DefaultPort ["+url.getDefaultPort()+"]");
        System.out.println("Host ["+url.getHost()+"]");
        System.out.println("Path ["+url.getPath()+"]");
        System.out.println("Port ["+url.getPort()+"]");
        System.out.println("Query ["+url.getQuery()+"]");
        System.out.println("Ref ["+url.getRef()+"]");
        System.out.println("UserInfo ["+url.getUserInfo()+"]");

        URLConnection c = null;
        try{
            c = url.openConnection();
            System.out.println("==================URLConnection info==================");
            //显示信息
            System.out.println("ContentType [" + c.getContentType() + "]");
            System.out.println("ContentEncoding [" + c.getContentEncoding() + "]");
            System.out.println("Content [" + c.getContent() + "]");
            System.out.println("ContentLength [" + c.getContentLength() + "]");
            System.out.println("Date [" + new Date(c.getDate()) + "]");
            System.out.println("LastModified [" + new Date(c.getExpiration()) + "]");
            System.out.println("Expiration [" + new Date(c.getLastModified()) + "]");

            if(c instanceof HttpURLConnection){
                System.out.println("==================HttpURLConnection info==================");
                HttpURLConnection httpURL = (HttpURLConnection)c;
                System.out.println("RequestMethod [" + httpURL.getRequestMethod() + "]");
                System.out.println("ResponseMessage [" + httpURL.getResponseMessage() + "]");
                System.out.println("ResponseCode [" + httpURL.getResponseCode() + "]");
                }
            }catch(IOException e){
            e.printStackTrace();
            }
        }

    public static void main(String[] args){
        try{
            String urlStr = "http://www.sina.com.cn:80/index.html";
            URL url = new URL(urlStr);
            printInfo(url);
        }catch(Exception e){
            System.err.println(e);
            }
        }
    }

运行结果:

G:\maul keyboard\network programming>javac GetURLInfo.java

G:\maul keyboard\network programming>java GetURLInfo
==================URL info==================
File [/index.html]
Protocol [http]
Authority [www.sina.com.cn:80]
DefaultPort [80]
Host [www.sina.com.cn]
Path [/index.html]
Port [80]
Query [null]
Ref [null]
UserInfo [null]
==================URLConnection info==================
ContentType [text/html]
ContentEncoding [null]
Content [[email protected]b9ce]
ContentLength [21826]
Date [Sat Sep 08 18:29:00 GMT+08:00 2018]
LastModified [Sat Sep 08 18:28:54 GMT+08:00 2018]
Expiration [Wed Apr 27 10:00:04 GMT+08:00 2005]
==================HttpURLConnection info==================
RequestMethod [GET]
ResponseMessage [OK]
ResponseCode [200]

G:\maul keyboard\network programming>

原文地址:https://www.cnblogs.com/celine/p/9610005.html

时间: 2024-11-06 16:05:46

获取URL相关信息的相关文章

web获取URL相关信息

http://www.xxx.com/project/path/user.do?param=1111 springMVC中返回如下 request.getRequestURL():返回http://www.xxx.com/project/path/user.do request.getRequestURI(): 返回/project/path/user.do request.getServletPath():  返回/path/user.do request.getQueryString():

获取IP相关信息和文件上传

获取IP相关信息 要获取用户访问者的IP地址相关信息,可以利用依赖注入,获取IHttpConnectionFeature的实例,从该实例上可以获取IP地址的相关信息,实例如下: var connection1 = Request.HttpContext.GetFeature<IHttpConnectionFeature>(); var connection2 = Context.GetFeature<IHttpConnectionFeature>(); var isLocal =

用javascript获取url网址信息

用javascript获取url网址信息 <script type="text/javascript">document.write("location.host="+location.host+"<br>");document.write("location.hostname="+location.hostname+"<br>");document.write(&quo

Linux sysinfo获取系统相关信息

Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <linux/unistd.h> /* for _syscallX macros/related stuff */ #include <linux/kernel.h> /* for struct sysinfo */ //_syscall1(int, sysi

PHP获取手机相关信息

该PHP操作类实现获取手机号手机头信息,取UA,取得手机类型,判断是否是opera,判断是否是m3gate,取得HA,取得手机IP 代码如下: <?php /** * @desc 手机操作类 获取手机相关信息 * @since */class mobile { /** * 函数名称: getPhoneNumber * 函数功能: 取手机号 * 输入参数: none * 函数返回值: 成功返回号码,失败返回false * 其它说明: 说明 */ public function getPhoneNu

Request获取url各种信息的方法

1.Request获取url各种信息的方法 测试的url地址:http://www.test.com/testweb/default.aspx, 结果如下: Request.ApplicationPath: /testweb Request.CurrentExecutionFilePath: /testweb/default.aspx Request.FilePath: /testweb/default.aspx Request.Path: /testweb/default.aspx Reque

借助Sigar API获取CPU相关信息

Sigar(全称System Information Gatherer And Reporter,即系统信息收集报表器),它提供了一个开源的跨平台的收集计算机硬件和操作系统信息的API(该API底层接口用C语言编写),本文将演示如何借助Sigar API获取CPU相关信息: package com.ghj.packageoftest; import org.hyperic.sigar.Cpu; import org.hyperic.sigar.CpuInfo; import org.hyperi

Java中获取系统相关信息——sigar

一.sigar简介 sigar中文名是系统信息收集和报表工具,是一个开源的工具,提供了跨平台的系统信息收集的API,可以和绝大多数操作系统和大多数版本打交道,可以收集的信息包括: 1.操作系统的信息,包括:dataModel.cpuEndian.name.version.arch.machine.description.patchLevel.vendor.vendorVersion.vendorName.vendorCodeName 2.CPU信息,包括:基本信息(vendor.model.mh

获取URL网页信息

static string GetHtml(string url) {string strHTML = ""; WebClient myWebClient = new WebClient(); Stream myStream = myWebClient.OpenRead(url); StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8")); strH