java.net.URL.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(172)

本文整理了Java中java.net.URL.<init>()方法的一些代码示例,展示了URL.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URL.<init>()方法的具体详情如下:
包路径:java.net.URL
类名称:URL
方法名:<init>

URL.<init>介绍

[英]Creates a new URL instance by parsing spec.
[中]通过解析规范创建新的URL实例。

代码示例

canonical example by Tabnine

public void postRequest(String urlStr, String jsonBodyStr) throws IOException {
  URL url = new URL(urlStr);
  HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  httpURLConnection.setDoOutput(true);
  httpURLConnection.setRequestMethod("POST");
  httpURLConnection.setRequestProperty("Content-Type", "application/json");
  try (OutputStream outputStream = httpURLConnection.getOutputStream()) { 
   outputStream.write(jsonBodyStr.getBytes());
   outputStream.flush();
  }
  if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
    try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))) {
      String line;
      while ((line = bufferedReader.readLine()) != null) {
        // ... do something with line
      }
    }
  } else {
    // ... do something with unsuccessful response
  }
}

代码示例来源:origin: stackoverflow.com

InputStream response = new URL(url).openStream();
// ...

代码示例来源:origin: stackoverflow.com

HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
httpConnection.setRequestMethod("POST");
// ...

代码示例来源:origin: stackoverflow.com

URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
// ...

代码示例来源:origin: stackoverflow.com

import java.net.*;
import java.io.*;

public class URLConnectionReader {
  public static void main(String[] args) throws Exception {
    URL yahoo = new URL("http://www.yahoo.com/");
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(
                new InputStreamReader(
                yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
      System.out.println(inputLine);
    in.close();
  }
}

代码示例来源:origin: stackoverflow.com

import java.net.*;
import java.io.*;

URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new InputStreamReader(
        whatismyip.openStream()));

String ip = in.readLine(); //you get the IP as a String
System.out.println(ip);

代码示例来源:origin: stackoverflow.com

URL url = new URL(user_image_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();   
conn.setDoInput(true);   
conn.connect();     
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);

代码示例来源:origin: skylot/jadx

private static <T> T get(String url, Type type) throws IOException {
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    if (con.getResponseCode() == 200) {
      Reader reader = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8);
      return GSON.fromJson(reader, type);
    }
    return null;
  }
}

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) throws Exception {
  String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
  String search = "stackoverflow";
  String charset = "UTF-8";

  URL url = new URL(google + URLEncoder.encode(search, charset));
  Reader reader = new InputStreamReader(url.openStream(), charset);
  GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);

  // Show title and URL of 1st result.
  System.out.println(results.getResponseData().getResults().get(0).getTitle());
  System.out.println(results.getResponseData().getResults().get(0).getUrl());
}

代码示例来源:origin: stackoverflow.com

String sURL = "http://freegeoip.net/json/"; //just a string
 // Connect to the URL using java's native library
 URL url = new URL(sURL);
 HttpURLConnection request = (HttpURLConnection) url.openConnection();
 request.connect();
 // Convert to a JSON object to print data
 JsonParser jp = new JsonParser(); //from gson
 JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element
 JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object. 
 zipcode = rootobj.get("zip_code").getAsString(); //just grab the zipcode

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
  "Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();

代码示例来源:origin: knowm/XChange

static ReturnCurrenciesResponse allCurrenciesStatic() throws IOException {
  HttpsURLConnection c =
    (HttpsURLConnection) new URL("https://api.idex.market/returnCurrencies").openConnection();
  c.setRequestMethod("POST");
  c.setRequestProperty("Accept-Encoding", "gzip");
  c.setRequestProperty("User-Agent", "irrelevant");
  try (InputStreamReader inputStreamReader =
    new InputStreamReader(new GZIPInputStream(c.getInputStream()))) {
   ObjectMapper objectMapper = new ObjectMapper();
   return objectMapper.readerFor(ReturnCurrenciesResponse.class).readValue(inputStreamReader);
  }
 }
}

代码示例来源:origin: stackoverflow.com

// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...

代码示例来源:origin: redisson/redisson

private static URLConnection fetchClass0(String host, int port,
                       String filename)
    throws IOException
  {
    URL url;
    try {
      url = new URL("http", host, port, filename);
    }
    catch (MalformedURLException e) {
      // should never reache here.
      throw new IOException("invalid URL?");
    }

    URLConnection con = url.openConnection();
    con.connect();
    return con;
  }
}

代码示例来源:origin: Bilibili/DanmakuFlameMaster

public void fillStreamFromHttpFile(Uri uri) {
  try {
    URL url = new URL(uri.getPath());
    url.openConnection();
    inStream = new BufferedInputStream(url.openStream());
  } catch (MalformedURLException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(
  httpCon.getOutputStream());
out.write("Resource content");
out.close();
httpCon.getInputStream();

代码示例来源:origin: stackoverflow.com

SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
URL url = new URL("https://gridserver:3049/cgi-bin/ls.py");
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setSSLSocketFactory(sslsocketfactory);
InputStream inputstream = conn.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

String string = null;
while ((string = bufferedreader.readLine()) != null) {
  System.out.println("Received " + string);
}

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://stackoverflow.com");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
  for (String line; (line = reader.readLine()) != null;) {
    System.out.println(line);
  }
}

代码示例来源:origin: stackoverflow.com

HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("HEAD");
int responseCode = connection.getResponseCode();
if (responseCode != 200) {
  // Not OK.
}

// < 100 is undetermined.
// 1nn is informal (shouldn't happen on a GET/HEAD)
// 2nn is success
// 3nn is redirect
// 4nn is client error
// 5nn is server error

代码示例来源:origin: dropwizard/dropwizard

@Override
  public InputStream open(String path) throws IOException {
    return new URL(path).openStream();
  }
}

相关文章