Search in sources :

Example 81 with NameValuePair

use of org.apache.http.NameValuePair in project wikidata-query-rdf by wikimedia.

the class WikibaseRepository method postWithToken.

/**
 * Post with a csrf token.
 *
 * @throws IOException if its thrown while communicating with wikibase
 */
private HttpPost postWithToken(URI uri) throws IOException {
    HttpPost request = new HttpPost(uri);
    List<NameValuePair> entity = new ArrayList<>();
    entity.add(new BasicNameValuePair("token", csrfToken()));
    request.setEntity(new UrlEncodedFormEntity(entity, Consts.UTF_8));
    return request;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 82 with NameValuePair

use of org.apache.http.NameValuePair in project instructure-android by instructure.

the class HttpHelpers method parseLinkHeaderResponse.

/**
 * parseLinkHeaderResponse is the old way of parsing the pagination URLs out of the response.
 * @param response
 * @return
 */
private static APIHttpResponse parseLinkHeaderResponse(HttpResponse response) {
    APIHttpResponse httpResponse = new APIHttpResponse();
    // Get status code.
    httpResponse.responseCode = response.getStatusLine().getStatusCode();
    // Check if response is supposed to have a body
    if (httpResponse.responseCode != 204) {
        try {
            httpResponse.responseBody = EntityUtils.toString(response.getEntity());
        } catch (Exception E) {
        }
    }
    Header[] linkHeader = response.getHeaders("Link");
    for (int j = 0; j < linkHeader.length; j++) {
        HeaderElement[] elements = linkHeader[j].getElements();
        for (int i = 0; i < elements.length; i++) {
            String first = elements[i].getName();
            String last = elements[i].getValue();
            // Seems to strip out the equals between name and value
            String url = first + "=" + last;
            if (url.startsWith("<") && url.endsWith(">")) {
                url = url.substring(1, url.length() - 1);
            } else {
                continue;
            }
            for (int k = 0; k < elements[i].getParameterCount(); k++) {
                NameValuePair nvp = elements[i].getParameter(k);
                if (nvp.getName().equals("rel")) {
                    if (nvp.getValue().equals("prev")) {
                        httpResponse.prevURL = url;
                    } else if (nvp.getValue().equals("next")) {
                        httpResponse.nextURL = url;
                    } else if (nvp.getValue().equals("first")) {
                        httpResponse.firstURL = url;
                    } else if (nvp.getValue().equals("last")) {
                        httpResponse.lastURL = url;
                    }
                }
            }
        }
    }
    return httpResponse;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement) IOException(java.io.IOException)

Example 83 with NameValuePair

use of org.apache.http.NameValuePair in project OsmAnd-tools by osmandapp.

the class BlockIO method doPostApiCall.

private Response doPostApiCall(String method, Map<String, String> params, Class<?> responseType) throws BlockIOException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost request = new HttpPost(Constants.buildUri(method, true));
    List<NameValuePair> postParams = new ArrayList<NameValuePair>(2);
    postParams.add(new BasicNameValuePair(Constants.Params.API_KEY, apiKey));
    if (params != null) {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
    }
    postParams.add(new BasicNameValuePair("priority", PRIORITY));
    CloseableHttpResponse response;
    try {
        request.setEntity(new UrlEncodedFormEntity(postParams));
        response = client.execute(request);
        return getResponse(response, responseType);
    } catch (IOException e) {
        throw new BlockIOException("Network connectivity problem.");
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) BlockIOException(net.osmand.bitcoinsender.utils.BlockIOException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BlockIOException(net.osmand.bitcoinsender.utils.BlockIOException)

Example 84 with NameValuePair

use of org.apache.http.NameValuePair in project jwt by emweb.

the class AuthUtils method parseFormUrlEncoded.

static void parseFormUrlEncoded(HttpMessage response, Map<String, String[]> parameters) {
    try {
        StringEntity entity = new StringEntity(response.getBody(), null);
        entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
        List<NameValuePair> valuePairs = URLEncodedUtils.parse(entity);
        for (NameValuePair nvp : valuePairs) {
            String[] values = new String[1];
            values[0] = nvp.getValue();
            parameters.put(nvp.getName(), values);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) NameValuePair(org.apache.http.NameValuePair) IOException(java.io.IOException)

Example 85 with NameValuePair

use of org.apache.http.NameValuePair in project ORCID-Source by ORCID.

the class OpenIDConnectTest method testImplicitWithNoAuthorizedGrant.

@Test
public void testImplicitWithNoAuthorizedGrant() throws URISyntaxException {
    HashMap<String, String> requestParams = new HashMap<String, String>();
    requestParams.put("nonce", "noMate");
    // check a client without implicit fails
    String clientId = getClient2ClientId();
    String clientRedirectUri = getClient2RedirectUri();
    String userName = getUser1OrcidId();
    String userPassword = getUser1Password();
    List<String> scope = Lists.newArrayList("openid");
    String nope = super.getImplicitTokenResponse(clientId, scope, userName, userPassword, clientRedirectUri, requestParams, "token", true);
    // check it's got a fragment
    assertTrue(nope.contains("#"));
    // switch to query param for ease of parsing
    nope = nope.replace('#', '?');
    List<NameValuePair> params = URLEncodedUtils.parse(new URI(nope), "UTF-8");
    Map<String, String> map = new HashMap<String, String>();
    for (NameValuePair pair : params) {
        map.put(pair.getName(), pair.getValue());
    }
    assertEquals(map.get("error"), "invalid_client");
    assertEquals(map.get("error_description"), "Unauthorized grant type: implicit");
}
Also used : NameValuePair(org.apache.http.NameValuePair) HashMap(java.util.HashMap) URI(java.net.URI) Test(org.junit.Test)

Aggregations

NameValuePair (org.apache.http.NameValuePair)713 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)592 ArrayList (java.util.ArrayList)478 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)256 HttpPost (org.apache.http.client.methods.HttpPost)218 HttpResponse (org.apache.http.HttpResponse)150 IOException (java.io.IOException)125 HttpEntity (org.apache.http.HttpEntity)108 Test (org.junit.Test)85 URI (java.net.URI)79 Map (java.util.Map)72 HashMap (java.util.HashMap)68 HttpGet (org.apache.http.client.methods.HttpGet)66 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)59 UnsupportedEncodingException (java.io.UnsupportedEncodingException)58 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)55 URISyntaxException (java.net.URISyntaxException)52 Document (org.jsoup.nodes.Document)51 URIBuilder (org.apache.http.client.utils.URIBuilder)50 HttpClient (org.apache.http.client.HttpClient)46