Search in sources :

Example 1 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project OpenRefine by OpenRefine.

the class RefineBroker method getUserId.

// ----------------------------------------------------------------------------------------
@SuppressWarnings("unchecked")
protected String getUserId(HttpServletRequest request) throws Exception {
    // This is useful for testing
    if (developmentMode) {
        return getParameter(request, "uid");
    }
    String oauth = request.getHeader(DELEGATED_OAUTH_HEADER);
    if (oauth == null) {
        throw new RuntimeException("The request needs to contain the '" + DELEGATED_OAUTH_HEADER + "' header set to obtain user identity via Freebase.");
    }
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    Map<String, String> params = (Map<String, String>) request.getParameterMap();
    for (Entry<String, String> e : params.entrySet()) {
        formparams.add(new BasicNameValuePair((String) e.getKey(), (String) e.getValue()));
    }
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
    HttpPost httpRequest = new HttpPost(USER_INFO_URL);
    httpRequest.setHeader(OAUTH_HEADER, oauth);
    httpRequest.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "OpenRefine Broker");
    httpRequest.setEntity(entity);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpRequest, responseHandler);
    JSONObject o = new JSONObject(responseBody);
    return o.getString("username");
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Map(java.util.Map)

Example 2 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project UltimateAndroid by cymcsg.

the class HttpUtils_Deprecated method getResponseStatusCodeFromPostUrl.

/*
     * 发送POST请求,通过URL和参数获取服务器反馈应答,通过返回的应答对象 在对数据头进行分析(如获得报文头 报文体等)
     */
public static Map getResponseStatusCodeFromPostUrl(String url, String logininfo, List<NameValuePair> params) throws Exception {
    Map rstmap = new HashMap();
    if (url.contains("http")) {
    } else {
        url = "http://t.qingdaonews.com" + url;
    }
    HttpPost httpRequest = new HttpPost(url);
    httpRequest.addHeader("Cookie", logininfo);
    if (null != params && params.size() > 0) {
        /* 添加请求参数到请求对象 */
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
    }
    HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
    StringBuffer sb = new StringBuffer();
    String inputLine = "";
    rstmap.put("code", httpResponse.getStatusLine().getStatusCode());
    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        InputStreamReader is = new InputStreamReader(httpResponse.getEntity().getContent());
        BufferedReader in = new BufferedReader(is);
        while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine);
        }
        in.close();
        rstmap.put("content", sb.toString());
    } else {
        rstmap.put("content", null);
    }
    return rstmap;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) HashMap(java.util.HashMap) Map(java.util.Map) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 3 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project tdi-studio-se by Talend.

the class RestClient method loginAs.

public void loginAs(String username, String password) {
    try {
        CookieStore cookieStore = new BasicCookieStore();
        httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        String loginURL = "/loginservice";
        // If you misspell a parameter you will get a HTTP 500 error
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("username", username));
        urlParameters.add(new BasicNameValuePair("password", password));
        urlParameters.add(new BasicNameValuePair("redirect", "false"));
        // UTF-8 is mandatory otherwise you get a NPE
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(urlParameters, "utf-8");
        executePostRequest(loginURL, entity);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) IOException(java.io.IOException)

Example 4 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project voltdb by VoltDB.

the class HTTPUtils method callProcOverJSONRaw.

public static String callProcOverJSONRaw(List<NameValuePair> vals, CloseableHttpClient httpclient, HttpPost httppost) throws Exception {
    HttpEntity entity = null;
    String entityStr = null;
    httppost.setEntity(new UrlEncodedFormEntity(vals));
    CloseableHttpResponse httpResponse = httpclient.execute(httppost);
    BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = reader.readLine()) != null) {
        response.append(inputLine);
    }
    return response.toString();
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 5 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project selenium-tests by Wikia.

the class CommonUtils method sendPost.

public static String sendPost(String apiUrl, String[][] param) {
    try {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(apiUrl);
        List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
        for (int i = 0; i < param.length; i++) {
            paramPairs.add(new BasicNameValuePair(param[i][0], param[i][1]));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(paramPairs));
        HttpResponse response = null;
        response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        return EntityUtils.toString(entity);
    } catch (UnsupportedEncodingException e) {
        PageObjectLogging.log("sendPost", e, false);
        return null;
    } catch (ClientProtocolException e) {
        PageObjectLogging.log("sendPost", e, false);
        return null;
    } catch (IOException e) {
        PageObjectLogging.log("sendPost", e, false);
        return null;
    }
}
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) HttpResponse(org.apache.http.HttpResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Aggregations

UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)402 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)353 HttpPost (org.apache.http.client.methods.HttpPost)317 ArrayList (java.util.ArrayList)290 NameValuePair (org.apache.http.NameValuePair)249 HttpResponse (org.apache.http.HttpResponse)195 IOException (java.io.IOException)140 HttpEntity (org.apache.http.HttpEntity)86 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)67 Map (java.util.Map)61 UnsupportedEncodingException (java.io.UnsupportedEncodingException)57 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)54 HttpClient (org.apache.http.client.HttpClient)52 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)51 ClientProtocolException (org.apache.http.client.ClientProtocolException)45 HttpGet (org.apache.http.client.methods.HttpGet)38 Test (org.junit.Test)32 URI (java.net.URI)30 HashMap (java.util.HashMap)29 Document (org.jsoup.nodes.Document)27