Search in sources :

Example 11 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project PneumaticCraft by MineMaarten.

the class PastebinHandler method loginInternal.

public boolean loginInternal(String userName, String password) {
    HttpPost httppost = new HttpPost("http://pastebin.com/api/api_login.php");
    List<NameValuePair> params = new ArrayList<NameValuePair>(3);
    params.add(new BasicNameValuePair("api_dev_key", DEV_KEY));
    params.add(new BasicNameValuePair("api_user_name", userName));
    params.add(new BasicNameValuePair("api_user_password", password));
    try {
        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            userKey = IOUtils.toString(instream, "UTF-8");
            if (userKey.startsWith("Bad API request")) {
                Log.warning("User tried to log in into pastebin, it responded with the following: " + userKey);
                userKey = null;
                return false;
            }
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException)

Example 12 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project 9GAG by Mixiaoxiao.

the class MxxHttpUtil method doHttpPost.

/**
	 * Op Http post request , "404error" response if failed
	 * 
	 * @param url
	 * @param map
	 *            Values to request
	 * @return
	 */
public static String doHttpPost(String url, HashMap<String, String> map) {
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT);
    HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT);
    ConnManagerParams.setTimeout(client.getParams(), TIMEOUT);
    HttpPost post = new HttpPost(url);
    post.setHeaders(headers);
    String result = "ERROR";
    ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
    if (map != null) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
            pairList.add(pair);
        }
    }
    try {
        HttpEntity entity = new UrlEncodedFormEntity(pairList, "UTF-8");
        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        } else {
            result = EntityUtils.toString(response.getEntity(), "UTF-8") + response.getStatusLine().getStatusCode() + "ERROR";
        }
    } catch (ConnectTimeoutException e) {
        result = "TIMEOUTERROR";
    } catch (Exception e) {
        result = "OTHERERROR";
        e.printStackTrace();
    }
    return result;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HashMap(java.util.HashMap) Map(java.util.Map) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 13 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project nanohttpd by NanoHttpd.

the class GetAndPostIntegrationTest method testPostRequestWithFormEncodedParameters.

@Test
public void testPostRequestWithFormEncodedParameters() throws Exception {
    this.testServer.response = "testPostRequestWithFormEncodedParameters";
    HttpPost httppost = new HttpPost("http://localhost:8192/");
    List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("age", "120"));
    postParameters.add(new BasicNameValuePair("gender", "Male"));
    httppost.setEntity(new UrlEncodedFormEntity(postParameters));
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = this.httpclient.execute(httppost, responseHandler);
    assertEquals("POST:testPostRequestWithFormEncodedParameters-params=2;age=120;gender=Male", responseBody);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Test(org.junit.Test)

Example 14 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair 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 15 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project FastDev4Android by jiangqqlmj.

the class IoUtils method sendMessageByPost.

/**
     * post投递
     */
public static void sendMessageByPost(String url, HashMap<String, String> map) {
    if (url == null || url.equals("")) {
        return;
    }
    Log.d(TAG_LISTLOGIC, "post投递地址:" + url);
    HttpPost httpPost = null;
    URI encodedUri = getEncodingURI(url);
    httpPost = new HttpPost(encodedUri);
    HttpClient httpClient = new DefaultHttpClient();
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DELIVER_CONN_TIMEOUT);
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, DELIVER_SOCKET_TIMEOUT);
    try {
        if (map != null) {
            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String key = entry.getKey().toString();
                String value = null;
                if (entry.getValue() == null) {
                    value = "";
                } else {
                    value = entry.getValue().toString();
                }
                Log.d(TAG_LISTLOGIC, "post投递参数" + key + "=" + value);
                BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, value);
                nameValuePair.add(basicNameValuePair);
            }
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8));
        }
        HttpResponse httpResponse = httpClient.execute(httpPost);
        if (httpResponse != null) {
            int code = httpResponse.getStatusLine().getStatusCode();
            if (code == HttpStatus.SC_OK) {
                Log.d(TAG_LISTLOGIC, "post投递服务器返回200");
                return;
            } else {
                httpPost.abort();
            }
        } else {
            httpPost.abort();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (httpClient != null) {
            httpClient.getConnectionManager().shutdown();
            httpClient = null;
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BasicNameValuePair (org.apache.http.message.BasicNameValuePair)279 NameValuePair (org.apache.http.NameValuePair)191 ArrayList (java.util.ArrayList)178 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)96 HttpPost (org.apache.http.client.methods.HttpPost)81 HttpResponse (org.apache.http.HttpResponse)75 HttpEntity (org.apache.http.HttpEntity)57 IOException (java.io.IOException)51 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)33 Test (org.junit.Test)31 HttpGet (org.apache.http.client.methods.HttpGet)29 ClientProtocolException (org.apache.http.client.ClientProtocolException)28 CSVReader (com.talend.csv.CSVReader)27 HttpClient (org.apache.http.client.HttpClient)24 JSONObject (org.json.JSONObject)20 UnsupportedEncodingException (java.io.UnsupportedEncodingException)19 HashMap (java.util.HashMap)19 Map (java.util.Map)17 URI (java.net.URI)16 WebserviceInvocation (com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation)15