Tabnine Logo
URLConnection.setRequestProperty
Code IndexAdd Tabnine to your IDE (free)

How to use
setRequestProperty
method
in
java.net.URLConnection

Best Java code snippets using java.net.URLConnection.setRequestProperty (Showing top 20 results out of 12,006)

Refine searchRefine arrow

  • URL.openConnection
  • URL.<init>
  • URLConnection.getInputStream
  • HttpURLConnection.setRequestMethod
  • URLConnection.setDoOutput
  • URLConnection.getOutputStream
origin: stackoverflow.com

 URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

try (OutputStream output = connection.getOutputStream()) {
  output.write(query.getBytes(charset));
}

InputStream response = connection.getInputStream();
// ...
origin: stackoverflow.com

 public static boolean hasActiveInternetConnection(Context context) {
  if (isNetworkAvailable(context)) {
    try {
      HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
      urlc.setRequestProperty("User-Agent", "Test");
      urlc.setRequestProperty("Connection", "close");
      urlc.setConnectTimeout(1500); 
      urlc.connect();
      return (urlc.getResponseCode() == 200);
    } catch (IOException e) {
      Log.e(LOG_TAG, "Error checking internet connection", e);
    }
  } else {
    Log.d(LOG_TAG, "No network available!");
  }
  return false;
}
origin: wildfly/wildfly

private InputStream streamOpener() throws IOException {
  final URL url = configurationUri.toURL();
  final URLConnection connection = url.openConnection();
  connection.setRequestProperty("Accept", "application/xml,text/xml,application/xhtml+xml");
  return connection.getInputStream();
}
origin: stackoverflow.com

 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");

FileBody fileBody = new FileBody(new File(fileName));
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
multipartEntity.addPart("file", fileBody);

connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue());
OutputStream out = connection.getOutputStream();
try {
  multipartEntity.writeTo(out);
} finally {
  out.close();
}
int status = connection.getResponseCode();
...
origin: stackoverflow.com

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

 HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
String encoded = Base64.encode(username+":"+password);
connection.setRequestProperty("Authorization", "Basic "+encoded);
origin: geoserver/geoserver

URLConnection conn = url.openConnection();
  conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
    return new GZIPInputStream(conn.getInputStream());
  } else if (encoding.equalsIgnoreCase("deflate")) {
    return new InflaterInputStream(conn.getInputStream(), new Inflater(true));
  } else {
    return conn.getInputStream();
origin: geotools/geotools

/**
 * @see org.geotools.data.ows.HTTPClient#post(java.net.URL, java.io.InputStream,
 *     java.lang.String)
 */
public HTTPResponse post(
    final URL url, final InputStream postContent, final String postContentType)
    throws IOException {
  URLConnection connection = openConnection(url);
  if (connection instanceof HttpURLConnection) {
    ((HttpURLConnection) connection).setRequestMethod("POST");
  }
  connection.setDoOutput(true);
  if (postContentType != null) {
    connection.setRequestProperty("Content-type", postContentType);
  }
  connection.connect();
  OutputStream outputStream = connection.getOutputStream();
  try {
    byte[] buff = new byte[512];
    int count;
    while ((count = postContent.read(buff)) > -1) {
      outputStream.write(buff, 0, count);
    }
  } finally {
    outputStream.flush();
    outputStream.close();
  }
  return new SimpleHTTPResponse(connection);
}
origin: stackoverflow.com

 HttpURLConnection httpUrlConnection = null;
URL url = new URL("http://example.com/server.cgi");
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setDoOutput(true);

httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
httpUrlConnection.setRequestProperty(
  "Content-Type", "multipart/form-data;boundary=" + this.boundary);
origin: stackoverflow.com

 import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;


public class TestUrlOpener {

  public static void main(String[] args) throws IOException {
    URL url = new URL("http://localhost:8080/foobar");
    URLConnection hc = url.openConnection();
    hc.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

    System.out.println(hc.getContentType());
  }

}
origin: wildfly/wildfly

static ConfigurationXMLStreamReader openUri(final URI uri, final XMLInputFactory xmlInputFactory) throws ConfigXMLParseException {
  try {
    final URL url = uri.toURL();
    final URLConnection connection = url.openConnection();
    connection.setRequestProperty("Accept", "application/xml,text/xml,application/xhtml+xml");
    final InputStream inputStream = connection.getInputStream();
    try {
      return openUri(uri, xmlInputFactory, inputStream);
    } catch (final Throwable t) {
      try {
        inputStream.close();
      } catch (Throwable t2) {
        t.addSuppressed(t2);
      }
      throw t;
    }
  } catch (MalformedURLException e) {
    throw msg.invalidUrl(new XMLLocation(uri), e);
  } catch (IOException e) {
    throw msg.failedToReadInput(new XMLLocation(uri), e);
  }
}
origin: stackoverflow.com

 URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
String userCredentials = "username:password";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
myURLConnection.setRequestProperty ("Authorization", basicAuth);
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
origin: stackoverflow.com

 URL url = new URL("http://www.example.com/comment");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization",
"Basic "+codec.encodeBase64String(("username:password").getBytes());
origin: org.codehaus.groovy/groovy

final URLConnection connection = url.openConnection();
if (parameters != null) {
  if (parameters.containsKey("connectTimeout")) {
    Map<String, CharSequence> properties = (Map<String, CharSequence>) parameters.get("requestProperties");
    for (Map.Entry<String, CharSequence> entry : properties.entrySet()) {
      connection.setRequestProperty(entry.getKey(), entry.getValue().toString());
return connection.getInputStream();
origin: stackoverflow.com

 String urlParameters  = "param1=a&param2=b&param3=c";
byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
int    postDataLength = postData.length;
String request        = "http://example.com/index.php";
URL    url            = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); 
conn.setRequestProperty( "charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
  wr.write( postData );
}
origin: stackoverflow.com

 public static boolean hasActiveInternetConnection(Context context) {
  if (isNetworkAvailable(context)) {
    try {
      HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
      urlc.setRequestProperty("User-Agent", "Test");
      urlc.setRequestProperty("Connection", "close");
      urlc.setConnectTimeout(1500); 
      urlc.connect();
      return (urlc.getResponseCode() == 200);
    } catch (IOException e) {
    Log.e(LOG_TAG, "Error checking internet connection", e);
    }
  } else {
  Log.d(LOG_TAG, "No network available!");
  }
  return false;
}
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: stackoverflow.com

 public static boolean hasInternetAccess(Context context) {
  if (isNetworkAvailable(context)) {
    try {
      HttpURLConnection urlc = (HttpURLConnection) 
        (new URL("http://clients3.google.com/generate_204")
        .openConnection());
      urlc.setRequestProperty("User-Agent", "Android");
      urlc.setRequestProperty("Connection", "close");
      urlc.setConnectTimeout(1500); 
      urlc.connect();
      return (urlc.getResponseCode() == 204 &&
            urlc.getContentLength() == 0);
    } catch (IOException e) {
      Log.e(TAG, "Error checking internet connection", e);
    }
  } else {
    Log.d(TAG, "No network available!");
  }
  return false;
}
origin: stackoverflow.com

URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
  OutputStream output = connection.getOutputStream();
  PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
) {
origin: stackoverflow.com

URL resourceUrl, base, next;
HttpURLConnection conn;
String location;
...
while (true)
{
  resourceUrl = new URL(url);
  conn        = (HttpURLConnection) resourceUrl.openConnection();
  conn.setConnectTimeout(15000);
  conn.setReadTimeout(15000);
  conn.setInstanceFollowRedirects(false);   // Make the logic below easier to detect redirections
  conn.setRequestProperty("User-Agent", "Mozilla/5.0...");
  switch (conn.getResponseCode())
  {
   case HttpURLConnection.HTTP_MOVED_PERM:
   case HttpURLConnection.HTTP_MOVED_TEMP:
     location = conn.getHeaderField("Location");
     base     = new URL(url);               
     next     = new URL(base, location);  // Deal with relative URLs
     url      = next.toExternalForm();
     continue;
  }
  break;
}
is = conn.openStream();
...
java.netURLConnectionsetRequestProperty

Javadoc

Sets the value of the specified request header field. The value will only be used by the current URLConnection instance. This method can only be called before the connection is established.

Popular methods of URLConnection

  • getInputStream
    Returns an input stream that reads from this open connection. A SocketTimeoutException can be thrown
  • setUseCaches
    Sets the value of the useCaches field of thisURLConnection to the specified value. Some protocols do
  • connect
    Opens a communications link to the resource referenced by this URL, if such a connection has not alr
  • getOutputStream
    Returns an output stream that writes to this connection.
  • getLastModified
    Returns the value of the last-modified header field. The result is the number of milliseconds since
  • setConnectTimeout
    Sets a specified timeout value, in milliseconds, to be used when opening a communications link to th
  • setDoOutput
    Sets the value of the doOutput field for thisURLConnection to the specified value. A URL connection
  • getContentLength
    Returns the value of the content-length header field.Note: #getContentLengthLong()should be preferre
  • setReadTimeout
    Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeou
  • getContentType
    Returns the value of the content-type header field.
  • getHeaderField
    Returns the value of the named header field. If called on a connection that sets the same header mul
  • setDoInput
    Sets the value of the doInput field for thisURLConnection to the specified value. A URL connection c
  • getHeaderField,
  • setDoInput,
  • addRequestProperty,
  • getURL,
  • getContentEncoding,
  • guessContentTypeFromName,
  • setDefaultUseCaches,
  • getFileNameMap,
  • getContent

Popular in Java

  • Parsing JSON documents to java classes using gson
  • scheduleAtFixedRate (Timer)
  • onRequestPermissionsResult (Fragment)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • HttpServer (com.sun.net.httpserver)
    This class implements a simple HTTP server. A HttpServer is bound to an IP address and port number a
  • Runnable (java.lang)
    Represents a command that can be executed. Often used to run code in a different Thread.
  • ConnectException (java.net)
    A ConnectException is thrown if a connection cannot be established to a remote host on a specific po
  • Comparator (java.util)
    A Comparator is used to compare two objects to determine their ordering with respect to each other.
  • JarFile (java.util.jar)
    JarFile is used to read jar entries and their associated data from jar files.
  • BasicDataSource (org.apache.commons.dbcp)
    Basic implementation of javax.sql.DataSource that is configured via JavaBeans properties. This is no
  • Best IntelliJ plugins
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