/** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET} * * @param parameters list of name/value pairs * @throws UnsupportedEncodingException if the default encoding isn't supported */ public UrlEncodedFormEntity ( final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException { super(URLEncodedUtils.format(parameters, HTTP.DEFAULT_CONTENT_CHARSET), HTTP.DEFAULT_CONTENT_CHARSET); setContentType(URLEncodedUtils.CONTENT_TYPE); }
/** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters in the specified encoding. * * @param parameters list of name/value pairs * @param encoding encoding the name/value pairs be encoded with * @throws UnsupportedEncodingException if the encoding isn't supported */ public UrlEncodedFormEntity ( final List <? extends NameValuePair> parameters, final String encoding) throws UnsupportedEncodingException { super(URLEncodedUtils.format(parameters, encoding), encoding); setContentType(URLEncodedUtils.CONTENT_TYPE); }
private <M extends HttpEntityEnclosingRequestBase> void addParameters(M method, String contentType, List<? extends NameValuePair> parameters) { UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(parameters, StandardCharsets.UTF_8); if (!contentType.trim().isEmpty()) { encodedFormEntity.setContentType(contentType); } method.setEntity(encodedFormEntity); }
/** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET} * * @param parameters list of name/value pairs * @throws UnsupportedEncodingException if the default encoding isn't supported */ public UrlEncodedFormEntity ( final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException { super(URLEncodedUtils.format(parameters, HTTP.DEFAULT_CONTENT_CHARSET), HTTP.DEFAULT_CONTENT_CHARSET); setContentType(URLEncodedUtils.CONTENT_TYPE); }
/** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET} * * @param parameters list of name/value pairs * @throws UnsupportedEncodingException if the default encoding isn't supported */ public UrlEncodedFormEntity ( final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException { super(URLEncodedUtils.format(parameters, HTTP.DEFAULT_CONTENT_CHARSET), HTTP.DEFAULT_CONTENT_CHARSET); setContentType(URLEncodedUtils.CONTENT_TYPE); }
public UrlEncodedFormEntity encodeForm( Map<?,?> params, Object contentType ) throws UnsupportedEncodingException { List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for ( Object key : params.keySet() ) { Object val = params.get( key ); if ( val instanceof List<?> ) for ( Object subVal : (List<?>)val ) paramList.add( new BasicNameValuePair( key.toString(), ( subVal == null ) ? "" : subVal.toString() ) ); else paramList.add( new BasicNameValuePair( key.toString(), ( val == null ) ? "" : val.toString() ) ); } UrlEncodedFormEntity e = new UrlEncodedFormEntity( paramList, charset.name() ); if ( contentType != null ) e.setContentType( contentType.toString() ); return e; }
/** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET} * * @param parameters list of name/value pairs * @throws UnsupportedEncodingException if the default encoding isn't supported */ public UrlEncodedFormEntity ( final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException { super(URLEncodedUtils.format(parameters, HTTP.DEFAULT_CONTENT_CHARSET), HTTP.DEFAULT_CONTENT_CHARSET); setContentType(URLEncodedUtils.CONTENT_TYPE); }
/** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters in the specified encoding. * * @param parameters list of name/value pairs * @param encoding encoding the name/value pairs be encoded with * @throws UnsupportedEncodingException if the encoding isn't supported */ public UrlEncodedFormEntity ( final List <? extends NameValuePair> parameters, final String encoding) throws UnsupportedEncodingException { super(URLEncodedUtils.format(parameters, encoding), encoding); setContentType(URLEncodedUtils.CONTENT_TYPE); }
/** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET} * * @param parameters list of name/value pairs * @throws UnsupportedEncodingException if the default encoding isn't supported */ public UrlEncodedFormEntity ( final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException { super(URLEncodedUtils.format(parameters, HTTP.DEFAULT_CONTENT_CHARSET), HTTP.DEFAULT_CONTENT_CHARSET); setContentType(URLEncodedUtils.CONTENT_TYPE); }
/** * 构建FormEntity * * @param formParam * @return * @throws UnsupportedEncodingException */ private static UrlEncodedFormEntity buildFormEntity(Map<String, String> formParam) throws UnsupportedEncodingException { if (formParam != null) { List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); for (String key : formParam.keySet()) { nameValuePairList.add(new BasicNameValuePair(key, formParam.get(key))); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, Constants.ENCODING); formEntity.setContentType(ContentType.CONTENT_TYPE_FORM); return formEntity; } return null; }
public UrlEncodedFormEntity encodeForm( Map<?,?> params, Object contentType ) throws UnsupportedEncodingException { List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for ( Object key : params.keySet() ) { Object val = params.get( key ); if ( val instanceof List<?> ) for ( Object subVal : (List<?>)val ) paramList.add( new BasicNameValuePair( key.toString(), ( subVal == null ) ? "" : subVal.toString() ) ); else paramList.add( new BasicNameValuePair( key.toString(), ( val == null ) ? "" : val.toString() ) ); } UrlEncodedFormEntity e = new UrlEncodedFormEntity( paramList, charset.name() ); if ( contentType != null ) e.setContentType( contentType.toString() ); return e; }
/** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters in the specified encoding. * * @param parameters list of name/value pairs * @param encoding encoding the name/value pairs be encoded with * @throws UnsupportedEncodingException if the encoding isn't supported */ public UrlEncodedFormEntity ( final List <? extends NameValuePair> parameters, final String encoding) throws UnsupportedEncodingException { super(URLEncodedUtils.format(parameters, encoding), encoding); setContentType(URLEncodedUtils.CONTENT_TYPE); }
/** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters in the specified encoding. * * @param parameters list of name/value pairs * @param encoding encoding the name/value pairs be encoded with * @throws UnsupportedEncodingException if the encoding isn't supported */ public UrlEncodedFormEntity ( final List <? extends NameValuePair> parameters, final String encoding) throws UnsupportedEncodingException { super(URLEncodedUtils.format(parameters, encoding), encoding); setContentType(URLEncodedUtils.CONTENT_TYPE); }
/** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters in the specified encoding. * * @param parameters list of name/value pairs * @param encoding encoding the name/value pairs be encoded with * @throws UnsupportedEncodingException if the encoding isn't supported */ public UrlEncodedFormEntity ( final List <? extends NameValuePair> parameters, final String encoding) throws UnsupportedEncodingException { super(URLEncodedUtils.format(parameters, encoding), encoding); setContentType(URLEncodedUtils.CONTENT_TYPE); }
/** * 封装 x-www-form-urlencoded 请求参数 * * @param bodyParams 请求参数 * @param httpMethod 请求方法 * @throws Exception 抛出异常 */ private static void packBodyParams(Map<String, String> bodyParams, HttpEntityEnclosingRequestBase httpMethod) throws Exception { if (null != bodyParams) { List<NameValuePair> nvps = new ArrayList<>(); Set<Map.Entry<String, String>> entrySet = bodyParams.entrySet(); for (Map.Entry<String, String> entry : entrySet) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } // 设置到请求的http对象中 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, ENCODING); entity.setContentType("application/x-www-form-urlencoded"); httpMethod.setEntity(entity); } }
/** * post form */ public static HttpResponse doPost(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, Map<String, String> bodys) throws Exception { HttpClient httpClient = wrapClient(host); HttpPost request = new HttpPost(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (bodys != null) { List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); for (String key : bodys.keySet()) { nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key))); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); request.setEntity(formEntity); } return httpClient.execute(request); }
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); request.setEntity(formEntity);
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); request.setEntity(formEntity);
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); request.setEntity(formEntity);
public void postData(final String email, final String password) { DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(LOGIN_URL); client.getParams().setParameter("http.useragent", "Custom Browser"); client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("email", email)); nameValuePairs.add(new BasicNameValuePair("password", password)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8); entity.setContentType("application/x-www-form-urlencoded"); post.setEntity(entity); printHeaders(client.execute(post)); printCookies(client.getCookieStore()); } catch (ClientProtocolException e) { LOG.error("ClientProtocolException", e); } catch (IOException e) { LOG.error("IOException", e); } }