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);
}
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;
}
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;
}
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();
}
}
}
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;
}
Aggregations