Search in sources :

Example 86 with NameValuePair

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

the class OpenIDConnectTest method testImplicitOauthAuthenticate.

@Test
public void testImplicitOauthAuthenticate() throws URISyntaxException, ParseException, JOSEException, JSONException, InvalidHashException {
    HashMap<String, String> requestParams = new HashMap<String, String>();
    requestParams.put("nonce", "yesMate");
    requestParams.put("state", "Boaty McBoatface");
    String response = getImplicitTokenResponse(Lists.newArrayList("/authenticate"), requestParams, true);
    // check it's got a fragment
    assertTrue(response.contains("#"));
    // switch to query param for ease of parsing
    response = response.replace('#', '?');
    List<NameValuePair> params = URLEncodedUtils.parse(new URI(response), "UTF-8");
    Map<String, String> map = new HashMap<String, String>();
    for (NameValuePair pair : params) {
        map.put(pair.getName(), pair.getValue());
    }
    // guid length
    assertEquals(map.get("access_token").length(), 36);
    assertTrue(map.get("id_token") == null);
}
Also used : NameValuePair(org.apache.http.NameValuePair) HashMap(java.util.HashMap) URI(java.net.URI) Test(org.junit.Test)

Example 87 with NameValuePair

use of org.apache.http.NameValuePair in project portal by ixinportal.

the class PersonalAuthenService method sendPost.

public static String sendPost(String url, String params, String jsonstr, String apiSecret) throws Exception {
    Mac mac = Mac.getInstance("HmacSHA1");
    SecretKeySpec signingKey = new SecretKeySpec(apiSecret.getBytes(), "HmacSHA1");
    mac.init(signingKey);
    byte[] rawHmac = mac.doFinal(jsonstr.getBytes("UTF-8"));
    String authCode = Base64.encodeBase64String(rawHmac);
    String body = "";
    // 创建httpclient对象
    CloseableHttpClient client = HttpClients.createDefault();
    // 创建post方式请求对象
    HttpPost httpPost = new HttpPost(url);
    // 装填参数
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair(params, jsonstr));
    // 设置参数到请求对象中
    httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
    // 设置header信息
    // 指定报文头【Content-type】、【User-Agent】
    // httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-Signature", "HMAC-SHA1 " + authCode);
    // 执行请求操作,并拿到结果(同步阻塞)
    CloseableHttpResponse response = client.execute(httpPost);
    // 获取结果实体
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        // 按指定编码转换结果实体为String类型
        body = EntityUtils.toString(entity, "UTF-8");
    }
    EntityUtils.consume(entity);
    // 释放链接
    response.close();
    return body;
}
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) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Mac(javax.crypto.Mac) SecretKeySpec(javax.crypto.spec.SecretKeySpec) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 88 with NameValuePair

use of org.apache.http.NameValuePair in project portal by ixinportal.

the class PersonalReviewServiceImpl method sendPost.

public static String sendPost(String url, String params, String jsonstr, String apiSecret) throws Exception {
    Mac mac = Mac.getInstance("HmacSHA1");
    SecretKeySpec signingKey = new SecretKeySpec(apiSecret.getBytes(), "HmacSHA1");
    mac.init(signingKey);
    byte[] rawHmac = mac.doFinal(jsonstr.getBytes("UTF-8"));
    String authCode = Base64.encodeBase64String(rawHmac);
    String body = "";
    // 创建httpclient对象
    CloseableHttpClient client = HttpClients.createDefault();
    // 创建post方式请求对象
    HttpPost httpPost = new HttpPost(url);
    // 装填参数
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair(params, jsonstr));
    // 设置参数到请求对象中
    httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
    // 设置header信息
    // 指定报文头【Content-type】、【User-Agent】
    // httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-Signature", "HMAC-SHA1 " + authCode);
    // 执行请求操作,并拿到结果(同步阻塞)
    CloseableHttpResponse response = client.execute(httpPost);
    // 获取结果实体
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        // 按指定编码转换结果实体为String类型
        body = EntityUtils.toString(entity, "UTF-8");
    }
    EntityUtils.consume(entity);
    // 释放链接
    response.close();
    return body;
}
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) HttpEntity(org.apache.http.HttpEntity) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Mac(javax.crypto.Mac) SecretKeySpec(javax.crypto.spec.SecretKeySpec) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 89 with NameValuePair

use of org.apache.http.NameValuePair in project weicoder by wdcode.

the class HttpAsyncClient method post.

/**
 * 模拟post提交
 * @param url post提交地址
 * @param data 提交参数
 * @param callback 回调结果
 * @param charset 编码
 */
public static void post(String url, Map<String, String> data, Callback<String> callback, String charset) {
    // 声明HttpPost
    HttpPost post = null;
    try {
        // 获得HttpPost
        post = new HttpPost(url);
        post.addHeader(new BasicHeader(HttpConstants.CONTENT_TYPE_KEY, HttpConstants.CONTENT_TYPE_VAL));
        // 如果参数列表为空 data为空map
        if (!EmptyUtil.isEmpty(data)) {
            // 声明参数列表
            List<NameValuePair> list = Lists.newList(data.size());
            // 设置参数
            for (Map.Entry<String, String> entry : data.entrySet()) {
                // 添加参数
                list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            // 设置参数与 编码格式
            post.setEntity(new UrlEncodedFormEntity(list, charset));
        }
        // 执行POST
        CLIENT.execute(post, new FutureCallback<HttpResponse>() {

            @Override
            public void failed(Exception ex) {
                Logs.error(ex);
            }

            @Override
            public void completed(HttpResponse result) {
                if (callback != null) {
                    try (InputStream in = result.getEntity().getContent()) {
                        callback.callback(IOUtil.readString(in));
                    } catch (Exception e) {
                        Logs.error(e);
                    }
                }
            }

            @Override
            public void cancelled() {
            }
        });
    } catch (Exception e) {
        Logs.error(e);
    } finally {
        // 销毁post
        if (post != null) {
            post.abort();
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOReactorException(org.apache.http.nio.reactor.IOReactorException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Map(java.util.Map) BasicHeader(org.apache.http.message.BasicHeader)

Example 90 with NameValuePair

use of org.apache.http.NameValuePair in project data-transfer-project by google.

the class ReferenceApiUtils method getRequestParams.

/**
 * Returns map of request parameters from the provided HttpExchange.
 */
public static Map<String, String> getRequestParams(HttpExchange exchange) {
    URIBuilder builder = new URIBuilder(exchange.getRequestURI());
    List<NameValuePair> queryParamPairs = builder.getQueryParams();
    Map<String, String> params = new HashMap<String, String>();
    for (NameValuePair pair : queryParamPairs) {
        params.put(pair.getName(), pair.getValue());
    }
    return params;
}
Also used : NameValuePair(org.apache.http.NameValuePair) HashMap(java.util.HashMap) URIBuilder(org.apache.http.client.utils.URIBuilder)

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