Search in sources :

Example 31 with HttpPost

use of org.apache.http.client.methods.HttpPost in project Anki-Android by Ramblurr.

the class HttpUtility method postReport.

public static Boolean postReport(String url, List<NameValuePair> values) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(values));
        HttpResponse response = httpClient.execute(httpPost);
        switch(response.getStatusLine().getStatusCode()) {
            case 200:
                Log.e(AnkiDroidApp.TAG, String.format("feedback report posted to %s", url));
                return true;
            default:
                Log.e(AnkiDroidApp.TAG, String.format("feedback report posted to %s message", url));
                Log.e(AnkiDroidApp.TAG, String.format("%d: %s", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
                break;
        }
    } catch (ClientProtocolException ex) {
        Log.e(AnkiDroidApp.TAG, ex.toString());
    } catch (IOException ex) {
        Log.e(AnkiDroidApp.TAG, ex.toString());
    }
    return false;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 32 with HttpPost

use of org.apache.http.client.methods.HttpPost in project Notes by MiCode.

the class GTaskClient method postRequest.

private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
    if (!mLoggedin) {
        Log.e(TAG, "please login first");
        throw new ActionFailureException("not logged in");
    }
    HttpPost httpPost = createHttpPost();
    try {
        LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
        list.add(new BasicNameValuePair("r", js.toString()));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
        httpPost.setEntity(entity);
        // execute the post
        HttpResponse response = mHttpClient.execute(httpPost);
        String jsString = getResponseContent(response.getEntity());
        return new JSONObject(jsString);
    } catch (ClientProtocolException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("postRequest failed");
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("postRequest failed");
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("unable to convert response content to jsonobject");
    } catch (Exception e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("error occurs when posting request");
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONException(org.json.JSONException) NetworkFailureException(net.micode.notes.gtask.exception.NetworkFailureException) IOException(java.io.IOException) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NetworkFailureException(net.micode.notes.gtask.exception.NetworkFailureException)

Example 33 with HttpPost

use of org.apache.http.client.methods.HttpPost 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 34 with HttpPost

use of org.apache.http.client.methods.HttpPost 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 35 with HttpPost

use of org.apache.http.client.methods.HttpPost in project nanohttpd by NanoHttpd.

the class GetAndPostIntegrationTest method testPostRequestWithEncodedParameters.

@Test
public void testPostRequestWithEncodedParameters() throws Exception {
    this.testServer.response = "testPostRequestWithEncodedParameters";
    HttpPost httppost = new HttpPost("http://localhost:8192/encodingtest");
    HttpResponse response = this.httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    String responseBody = EntityUtils.toString(entity);
    assertEquals("<html><head><title>Testé ça</title></head><body>Testé ça</body></html>", responseBody);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Aggregations

HttpPost (org.apache.http.client.methods.HttpPost)531 HttpResponse (org.apache.http.HttpResponse)238 StringEntity (org.apache.http.entity.StringEntity)220 Test (org.junit.Test)153 IOException (java.io.IOException)143 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)107 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)99 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)90 ArrayList (java.util.ArrayList)86 NameValuePair (org.apache.http.NameValuePair)84 HttpEntity (org.apache.http.HttpEntity)83 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)68 HttpClient (org.apache.http.client.HttpClient)64 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)63 HttpGet (org.apache.http.client.methods.HttpGet)55 TestHttpClient (io.undertow.testutils.TestHttpClient)54 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)49 JsonNode (com.fasterxml.jackson.databind.JsonNode)39 InputStream (java.io.InputStream)29