Search in sources :

Example 26 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project jstorm by alibaba.

the class AlimonitorClient method httpPost.

private boolean httpPost(String url, String msg) {
    boolean ret = false;
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    CloseableHttpResponse response = null;
    try {
        HttpPost request = new HttpPost(url);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("name", monitorName));
        nvps.add(new BasicNameValuePair("msg", msg));
        request.setEntity(new UrlEncodedFormEntity(nvps));
        response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            LOG.info(EntityUtils.toString(entity));
        }
        EntityUtils.consume(entity);
        ret = true;
    } catch (Exception e) {
        LOG.error("Exception when sending http request to alimonitor", e);
    } finally {
        try {
            if (response != null)
                response.close();
            httpClient.close();
        } catch (Exception e) {
            LOG.error("Exception when closing httpclient", e);
        }
    }
    return ret;
}
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) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 27 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project camel by apache.

the class RestletPostFormTest method testPostBody.

@Test
public void testPostBody() throws Exception {
    HttpUriRequest method = new HttpPost("http://localhost:" + portNum + "/users");
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("foo", "bar"));
    ((HttpEntityEnclosingRequestBase) method).setEntity(new UrlEncodedFormEntity(urlParameters));
    HttpResponse response = doExecute(method);
    assertHttpResponse(response, 200, "text/plain");
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Test(org.junit.Test)

Example 28 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project android-instagram by markchang.

the class ImageListActivity method postComment.

public void postComment(String comment, InstagramImage image, String username) {
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new BasicNameValuePair("comment_text", comment));
    String jsonResponse = Utils.doRestulPut(httpClient, Utils.createCommentUrl(image.pk), postParams, this);
    if (jsonResponse != null) {
        image.comment_list.add(new Comment(username, comment));
        Toast.makeText(this, "Comment successful", Toast.LENGTH_SHORT).show();
        adapter.notifyDataSetChanged();
    } else {
        Toast.makeText(this, "Comment failed", Toast.LENGTH_SHORT).show();
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList)

Example 29 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project android-instagram by markchang.

the class TakePictureActivity method doUpload.

// fixme: this doesn't need to be a Map return
public Map<String, String> doUpload() {
    Log.i(TAG, "Upload");
    Long timeInMilliseconds = System.currentTimeMillis() / 1000;
    String timeInSeconds = timeInMilliseconds.toString();
    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    Map returnMap = new HashMap<String, String>();
    // check for cookies
    if (httpClient.getCookieStore() == null) {
        returnMap.put("result", "Not logged in");
        return returnMap;
    }
    try {
        // create multipart data
        File imageFile = new File(processedImageUri.getPath());
        FileBody partFile = new FileBody(imageFile);
        StringBody partTime = new StringBody(timeInSeconds);
        multipartEntity.addPart("photo", partFile);
        multipartEntity.addPart("device_timestamp", partTime);
    } catch (Exception e) {
        Log.e(TAG, "Error creating mulitpart form: " + e.toString());
        returnMap.put("result", "Error creating mulitpart form: " + e.toString());
        return returnMap;
    }
    // upload
    try {
        HttpPost httpPost = new HttpPost(Utils.UPLOAD_URL);
        httpPost.setEntity(multipartEntity);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        Log.i(TAG, "Upload status: " + httpResponse.getStatusLine());
        // test result code
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            Log.e(TAG, "Login HTTP status fail: " + httpResponse.getStatusLine().getStatusCode());
            returnMap.put("result", "HTTP status error: " + httpResponse.getStatusLine().getStatusCode());
            return returnMap;
        }
        /*
            {"status": "ok"}
            */
        if (httpEntity != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"));
            String json = reader.readLine();
            JSONTokener jsonTokener = new JSONTokener(json);
            JSONObject jsonObject = new JSONObject(jsonTokener);
            Log.i(TAG, "JSON: " + jsonObject.toString());
            String loginStatus = jsonObject.getString("status");
            if (!loginStatus.equals("ok")) {
                Log.e(TAG, "JSON status not ok: " + jsonObject.getString("status"));
                returnMap.put("result", "JSON status not ok: " + jsonObject.getString("status"));
                return returnMap;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "HttpPost exception: " + e.toString());
        returnMap.put("result", "HttpPost exception: " + e.toString());
        return returnMap;
    }
    // configure / comment
    try {
        HttpPost httpPost = new HttpPost(Utils.CONFIGURE_URL);
        String partComment = txtCaption.getText().toString();
        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        postParams.add(new BasicNameValuePair("device_timestamp", timeInSeconds));
        postParams.add(new BasicNameValuePair("caption", partComment));
        httpPost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        // test result code
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            Log.e(TAG, "Upload comment fail: " + httpResponse.getStatusLine().getStatusCode());
            returnMap.put("result", "Upload comment fail: " + httpResponse.getStatusLine().getStatusCode());
            return returnMap;
        }
        returnMap.put("result", "ok");
        return returnMap;
    } catch (Exception e) {
        Log.e(TAG, "HttpPost comment error: " + e.toString());
        returnMap.put("result", "HttpPost comment error: " + e.toString());
        return returnMap;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project glitch-hq-android by tinyspeck.

the class Glitch method login.

public void login(String emailAddress, String password, final GlitchSessionDelegate delegate) {
    String sURL = "http://api.glitch.com/oauth2/token";
    Vector<BasicNameValuePair> params = new Vector<BasicNameValuePair>(6);
    params.add(new BasicNameValuePair("username", emailAddress));
    params.add(new BasicNameValuePair("password", password));
    params.add(new BasicNameValuePair("grant_type", "password"));
    params.add(new BasicNameValuePair("scope", "write"));
    params.add(new BasicNameValuePair("client_id", "197-764ef7f4f676f4b53819e52ea7cca4c65badf353"));
    params.add(new BasicNameValuePair("client_secret", "584d266525e5d8d7cf17faf9391f0145f12b12c6"));
    new ServerRequestTask(sURL, true, params) {

        protected void onPostExecute(String result) {
            if (result == null) {
                delegate.glitchConnectionError();
                return;
            }
            Log.i("glitch", result);
            if (result.length() == 0) {
                delegate.glitchLoginFail();
                return;
            }
            try {
                JSONTokener tokener = new JSONTokener(result);
                JSONObject jObject = new JSONObject(tokener);
                accessToken = (String) jObject.get("access_token");
                delegate.glitchLoginSuccess();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }.execute();
}
Also used : JSONTokener(org.json.JSONTokener) ServerRequestTask(com.tinyspeck.glitchhq.ServerRequestTask) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) JSONException(org.json.JSONException) Vector(java.util.Vector)

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