Search in sources :

Example 86 with HttpPost

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

the class Olingo2AppImpl method batch.

@Override
public void batch(Edm edm, Object data, Olingo2ResponseHandler<List<Olingo2BatchResponse>> responseHandler) {
    final UriInfoWithType uriInfo = parseUri(edm, BATCH, null);
    writeContent(edm, new HttpPost(createUri(BATCH, null)), uriInfo, data, responseHandler);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost)

Example 87 with HttpPost

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

the class Olingo2AppImpl method create.

@Override
public <T> void create(Edm edm, String resourcePath, Object data, Olingo2ResponseHandler<T> responseHandler) {
    final UriInfoWithType uriInfo = parseUri(edm, resourcePath, null);
    writeContent(edm, new HttpPost(createUri(resourcePath, null)), uriInfo, data, responseHandler);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost)

Example 88 with HttpPost

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

use of org.apache.http.client.methods.HttpPost in project android-instagram by markchang.

the class Utils method doRestulPut.

public static String doRestulPut(DefaultHttpClient httpClient, String url, List<NameValuePair> postParams, Context ctx) {
    // create POST
    HttpPost httpPost = new HttpPost(url);
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        // test result code
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            Log.i(TAG, "Login HTTP status fail");
            return null;
        }
        // test json response
        HttpEntity httpEntity = httpResponse.getEntity();
        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"));
                return null;
            } else {
                return json;
            }
        } else {
            return null;
        }
    } catch (IOException e) {
        Log.e(TAG, "HttpPost error: " + e.toString());
        return null;
    } catch (JSONException e) {
        Log.e(TAG, "JSON parse error: " + e.toString());
        return null;
    }
}
Also used : JSONTokener(org.json.JSONTokener) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) JSONObject(org.json.JSONObject) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 90 with HttpPost

use of org.apache.http.client.methods.HttpPost in project okhttp by square.

the class OkApacheClientTest method postOverrideContentType.

@Test
public void postOverrideContentType() throws Exception {
    server.enqueue(new MockResponse());
    HttpPost httpPost = new HttpPost();
    httpPost.setURI(server.url("/").url().toURI());
    httpPost.addHeader("Content-Type", "application/xml");
    httpPost.setEntity(new StringEntity("<yo/>"));
    client.execute(httpPost);
    RecordedRequest request = server.takeRequest();
    assertEquals(request.getHeader("Content-Type"), "application/xml");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) 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