Tabnine Logo
URL.<init>
Code IndexAdd Tabnine to your IDE (free)

How to use
java.net.URL
constructor

Best Java code snippets using java.net.URL.<init> (Showing top 20 results out of 82,647)

Refine searchRefine arrow

  • URL.openConnection
  • URLConnection.getInputStream
  • InputStreamReader.<init>
  • BufferedReader.<init>
  • HttpURLConnection.setRequestMethod
  • BufferedReader.readLine
  • URL.openStream
  • URLConnection.connect
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();
  }
}
java.netURL<init>

Javadoc

Creates a new URL instance by parsing spec.

Popular methods of URL

  • openStream
    Opens a connection to this URL and returns anInputStream for reading from that connection. This meth
  • openConnection
    Returns a new connection to the resource referred to by this URL.
  • toString
    Constructs a string representation of this URL. The string is created by calling the toExternalForm
  • getPath
    Gets the path part of this URL.
  • toURI
    Returns a java.net.URI equivalent to this URL. This method functions in the same way as new URI (thi
  • getProtocol
    Gets the protocol name of this URL.
  • getFile
    Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the c
  • toExternalForm
    Constructs a string representation of this URL. The string is created by calling the toExternalForm
  • getHost
    Gets the host name of this URL, if applicable. The format of the host conforms to RFC 2732, i.e. for
  • getPort
    Gets the port number of this URL.
  • getQuery
    Gets the query part of this URL.
  • equals
    Compares this URL for equality with another object. If the given object is not a URL then this metho
  • getQuery,
  • equals,
  • getRef,
  • getUserInfo,
  • getAuthority,
  • hashCode,
  • getDefaultPort,
  • getContent,
  • setURLStreamHandlerFactory

Popular in Java

  • Reading from database using SQL prepared statement
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • getExternalFilesDir (Context)
  • notifyDataSetChanged (ArrayAdapter)
  • GregorianCalendar (java.util)
    GregorianCalendar is a concrete subclass of Calendarand provides the standard calendar used by most
  • Locale (java.util)
    Locale represents a language/country/variant combination. Locales are used to alter the presentatio
  • TreeSet (java.util)
    TreeSet is an implementation of SortedSet. All optional operations (adding and removing) are support
  • JButton (javax.swing)
  • Scheduler (org.quartz)
    This is the main interface of a Quartz Scheduler. A Scheduler maintains a registry of org.quartz.Job
  • SAXParseException (org.xml.sax)
    Encapsulate an XML parse error or warning.> This module, both source code and documentation, is in t
  • CodeWhisperer alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now