HttpURLConnection setRequestMethod - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for HttpURLConnection setRequestMethod method.

This code example shows how to use the following methods:setRequestMethod, setRequestProperty, setSSLSocketFactory
      SSLContext context = SSLContext.getInstance("TLS");
      context.init(null, trustManagerFactory.getTrustManagers(), null);
 
      URL url = new URL(String.format("https://gcm.textsecure.whispersystems.org%s", urlFragment));
      HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
      connection.setSSLSocketFactory(context.getSocketFactory());
      connection.setRequestMethod(method);
      connection.setRequestProperty("Content-Type", "application/json");
 
      if (password != null) {
        System.out.println("Adding authorization header: " + getAuthorizationHeader());
        connection.setRequestProperty("Authorization", getAuthorizationHeader());
      } 
 
      return connection;
    } catch (NoSuchAlgorithmException e) {
      throw new AssertionError(e);
    } catch (KeyManagementException e) {
      throw new AssertionError(e);
    } catch (MalformedURLException e) {
8
CodeRank
This code example shows how to use the following methods:disconnect, getResponseCode, setChunkedStreamingMode, setRequestMethod, getOutputStream
This code example shows how to use the following methods:setRequestMethod, setDoInput, setDoOutput, setRequestProperty, setUseCaches
		} 
		return new Request.Builder().source("com.apple.maps").unknown3(0).unknown4(0).wifis(wifis).build(); 
	} 
 
	private static void prepareConnection(HttpsURLConnection connection, int length) throws ProtocolException {
		connection.setRequestMethod("POST");
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty(HTTP_FIELD_CONTENT_TYPE, CONTENT_TYPE_URLENCODED);
		connection.setRequestProperty(HTTP_FIELD_CONTENT_LENGTH, String.valueOf(length));
	} 
 
	public Response retrieveLocations(String... macs) throws IOException {
		Request request = createRequest(macs);
		byte[] byteb = request.toByteArray();
		byte[] bytes = combineBytes(APPLE_MAGIC_BYTES, byteb, (byte) byteb.length);
		HttpsURLConnection connection = createConnection();
		prepareConnection(connection, bytes.length);
		OutputStream out = connection.getOutputStream();
7
CodeRank
This code example shows how to use the following methods:setRequestMethod
	 */ 
	public static HttpURLConnection post(String url, Map<String, Object> parts)
			throws IOException { 
		HttpURLConnection post = (HttpURLConnection) new URL(url)
				.openConnection();
		post.setRequestMethod("POST"); //$NON-NLS-1$
		return post(post, parts);
	} 
 
	/** 
	 * Post parts to connection 
	 * 
	 * @param post 
	 * @param parts 
	 * @return connection that was posted to 
	 * @throws IOException 
	 */ 
	public static HttpURLConnection post(HttpURLConnection post,
			Map<String, Object> parts) throws IOException {
		String boundary = "00content0boundary00"; //$NON-NLS-1$
7
CodeRank
This code example shows how to use the following methods:setRequestMethod, getOutputStream, setDoOutput, setRequestProperty
    } 
 
    public static URLConnection httpPost(WebServer webServer, String path, String body) throws IOException {
        URL url = new URL(webServer.getUri().toURL(), path);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setDoOutput(true);
        urlConnection.getOutputStream().write(body.getBytes(Charset.forName("UTF8")));
        return urlConnection;
    } 
 
    public static URLConnection httpPostCompressed(WebServer webServer, String path, String body) throws IOException {
        URL url = new URL(webServer.getUri().toURL(), path);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.addRequestProperty("Content-Encoding", "gzip");
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setDoOutput(true);
 
8
CodeRank
This code example shows how to use the following methods:setRequestMethod, getHeaderField, setConnectTimeout, setReadTimeout
		return this.connection.getContentEncoding();
	} 
	 
	public String getHeaderField(String name) throws IOException {
		connect(); 
		return this.connection.getHeaderField(name);
	} 
	 
	public String getHeaderField(int n) throws IOException {
		connect(); 
		return this.connection.getHeaderField(n);
	} 
	 
	public int getResponseCode()  throws IOException { 
		connect(); 
		int responseCode = this.connection.getResponseCode();
		return responseCode;
	} 
	 
	public String getResponseMessage() throws IOException {
7
CodeRank
This code example shows how to use the following methods:setRequestMethod, setConnectTimeout, setDoInput, setDoOutput, setReadTimeout
    } 
 
    private HttpURLConnection openConnection(URL targetUrl) throws IOException {
        HttpURLConnection conn = (HttpURLConnection) targetUrl.openConnection();
        if (0 < getTimeoutMillis()) { 
            conn.setConnectTimeout(getTimeoutMillis());
            conn.setReadTimeout(getTimeoutMillis());
        } 
        if (isTryGzip()) { 
            conn.setRequestProperty("Accept-Encoding", "gzip");
        } 
 
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        return conn;
    } 
} 
7
CodeRank
This code example shows how to use the following methods:getResponseCode, setRequestMethod, connect, setRequestProperty
 
            String userInfo = url.getUserInfo();
            if(userInfo != null) {
                byte[] authEncBytes = Base64.encode(userInfo.getBytes(), Base64.DEFAULT);
 
                conn.setRequestProperty("Authorization", "Basic " + new String(authEncBytes));
            } 
 
            conn.setRequestMethod("DELETE");
            conn.connect();
            int responseCode = conn.getResponseCode();
            Assert.assertTrue(responseCode < 300 || responseCode == 404);
        } catch (Exception e) {
            Log.e(TAG, "Exceptiong deleting remote db", e);
        } 
    } 
 
    protected TDURLConnection sendRequest(TDServer server, String method, String path, Map<String,String> headers, Object bodyObj) {
        try { 
            URL url = new URL("touchdb://" + path);
8
CodeRank
This code example shows how to use the following methods:setRequestMethod, getHeaderField, setDoInput
            } 
            case URI_TYPE_HTTP:
            case URI_TYPE_HTTPS: {
                try { 
                    HttpURLConnection conn = httpClient.open(new URL(uri.toString()));
                    conn.setDoInput(false);
                    conn.setRequestMethod("HEAD");
                    return conn.getHeaderField("Content-Type");
                } catch (IOException e) {
                } 
            } 
        } 
         
        return null; 
    } 
     
    private String getMimeTypeFromPath(String path) {
        String extension = path;
        int lastDot = extension.lastIndexOf('.');
        if (lastDot != -1) {
7
CodeRank
This code example shows how to use the following methods:setRequestMethod, getInputStream, setRequestProperty
	public static String useragent;
 
	/** mainly sets the User-Agent we need */ 
	private static void setUserAgent(HttpURLConnection conn) {
		if (useragent != null)
			conn.setRequestProperty("User-Agent", useragent);
	} 
 
	public static URL getRedirectedUrl(URL url) throws IOException {
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		setUserAgent(conn);
		conn.setRequestMethod("GET");
		conn.setInstanceFollowRedirects(false);
		int rc = conn.getResponseCode();
		if (rc != REDIRECT_RESPONSE_CODE) {
			throw new IOException("code " + rc + " '" + conn.getResponseMessage() + "'");
		} 
		String location = conn.getHeaderField("Location");
		if (location == null) {
			throw new IOException("No 'Location' header found");

7
CodeRank

Related HttpURLConnection setRequestMethod Questions & Answers:

Other HttpURLConnection setRequestMethod Examples

99:	            String message = context.getString(R.string.server_unregister_error,
100:	                    e.getMessage());
101:	            CommonUtilities.displayMessage(context, message);
102:	        }
103:	    }

Read More

setRequestMethod Method Overview
Sets the request command which will be sent to the remote HTTP server.
See Code Examples for other Java 8 HttpURLConnection Methods: