Search in sources :

Example 56 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project AndroidSDK-RecipeBook by gabu.

the class Recipe095 method uploadForTumblr.

// 指定されたUriの写真をTumblrにアップロードします。
private void uploadForTumblr(Uri uri) {
    // HTTPクライアントを作って
    HttpClient client = new DefaultHttpClient();
    // POST先のURLを指定してPOSTオブジェクトを作って
    HttpPost post = new HttpPost("http://www.tumblr.com/api/write");
    // パラメータを作って
    MultipartEntity entity = new MultipartEntity();
    try {
        // Thumblrに登録したメールアドレス
        entity.addPart("email", new StringBody("hoge@example.com"));
        // Thumblrに登録したパスワード
        entity.addPart("password", new StringBody("1234"));
        // 投稿する種類。今回は写真なのでphoto
        entity.addPart("type", new StringBody("photo"));
        // 写真データ
        entity.addPart("data", new InputStreamBody(getContentResolver().openInputStream(uri), "filename"));
        // POSTオブジェクトにパラメータをセット
        post.setEntity(entity);
        // POSTリクエストを実行
        client.execute(post);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) FileNotFoundException(java.io.FileNotFoundException) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 57 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project h2o-2 by h2oai.

the class GoogleAnalyticsThreadFactory method post.

@SuppressWarnings({ "rawtypes" })
public GoogleAnalyticsResponse post(GoogleAnalyticsRequest request) {
    GoogleAnalyticsResponse response = new GoogleAnalyticsResponse();
    if (!config.isEnabled()) {
        return response;
    }
    BasicHttpResponse httpResponse = null;
    try {
        List<NameValuePair> postParms = new ArrayList<NameValuePair>();
        //Log.debug("GA Processing " + request);
        //Process the parameters
        processParameters(request, postParms);
        //Process custom dimensions
        processCustomDimensionParameters(request, postParms);
        //Process custom metrics
        processCustomMetricParameters(request, postParms);
        //Log.debug("GA Processed all parameters and sending the request " + postParms);
        HttpPost httpPost = new HttpPost(config.getUrl());
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(postParms, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            Log.warn("This systems doesn't support UTF-8!");
        }
        try {
            httpResponse = (BasicHttpResponse) httpClient.execute(httpPost);
        } catch (ClientProtocolException e) {
        //Log.trace("GA connectivity had a problem or the connectivity was aborted.  "+e.toString());
        } catch (IOException e) {
        //Log.trace("GA connectivity suffered a protocol error.  "+e.toString());
        }
        //Log.debug("GA response: " +httpResponse.toString());
        response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
        response.setPostedParms(postParms);
        try {
            EntityUtils.consume(httpResponse.getEntity());
        } catch (IOException e) {
        /*consume quietly*/
        }
        if (config.isGatherStats()) {
            gatherStats(request);
        }
    } catch (Exception e) {
        if (e instanceof UnknownHostException) {
        //Log.trace("Coudln't connect to GA. Internet may not be available. " + e.toString());
        } else {
        //Log.trace("Exception while sending the GA tracker request: " + request +".  "+ e.toString());
        }
    }
    return response;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 58 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project musiccabinet by hakko.

the class AbstractWSPostClient method executeWSRequest.

/*
	 * Executes a request to a Last.fm web service.
	 * 
	 * When adding support for a new web service, a class extending this should be
	 * implemented. The web service can then be invoked by calling this method, using
	 * relevant parameters.
	 * 
	 * The parameter api_key, which is identical for all web service invocations, is
	 * automatically included.
	 * 
	 * The response is bundled in a WSResponse object, with eventual error code/message.
	 * 
	 * Note: For non US-ASCII characters, Last.fm distinguishes between upper and lower
	 * case. Make sure to use proper capitalization.
	 */
protected WSResponse executeWSRequest(List<NameValuePair> params) throws ApplicationException {
    authenticateParameterList(params);
    WSResponse wsResponse;
    HttpClient httpClient = getHttpClient();
    try {
        HttpPost httpPost = new HttpPost(getURI(params));
        httpPost.setEntity(new UrlEncodedFormEntity(params, CharSet.UTF8));
        HttpResponse response = httpClient.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        HttpEntity responseEntity = response.getEntity();
        String responseBody = EntityUtils.toString(responseEntity);
        EntityUtils.consume(responseEntity);
        LOG.debug("post responseBody: " + responseBody);
        wsResponse = (statusCode == 200) ? new WSResponse(responseBody) : new WSResponse(isHttpRecoverable(statusCode), statusCode, responseBody);
    } catch (ClientProtocolException e) {
        throw new ApplicationException("The request to post data to Last.fm could not be completed!", e);
    } catch (IOException e) {
        LOG.warn("Could not post data to Last.fm!", e);
        wsResponse = new WSResponse(true, -1, "Call failed due to " + e.getMessage());
    }
    return wsResponse;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) HttpEntity(org.apache.http.HttpEntity) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 59 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project Java-Mandrill-Wrapper by cribbstechnologies.

the class MandrillRESTRequestTest method testPostRequestNon200Response.

@Test
public void testPostRequestNon200Response() {
    try {
        this.request = new MandrillRESTRequest();
        this.request.setHttpClient(this.client);
        this.request.setConfig(this.config);
        this.request.setObjectMapper(this.mapper);
        doReturn("postData").when(this.mapper).writeValueAsString(this.emptyBaseRequest);
        doReturn(this.response).when(this.client).execute(isA(HttpPost.class));
        doReturn(this.manager).when(this.client).getConnectionManager();
        Mockito.when(this.response.getEntity()).thenReturn(this.entity);
        InputStream inputStream = IOUtils.toInputStream("INPUT");
        Mockito.when(this.entity.getContent()).thenReturn(inputStream);
        Mockito.when(this.response.getStatusLine()).thenReturn(this.statusLine);
        Mockito.when(this.statusLine.getStatusCode()).thenReturn(500);
        this.request.postRequest(this.emptyBaseRequest, "Foo", null);
    } catch (RequestFailedException rfe) {
        assertEquals("Failed : HTTP error code : 500 INPUT", rfe.getMessage());
    } catch (ClientProtocolException e) {
        fail("Mockito is a good mocking framework, this shouldn't happen");
    } catch (IOException e) {
        fail("Mockito is a good mocking framework, this shouldn't happen");
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestFailedException(com.cribbstechnologies.clients.mandrill.exception.RequestFailedException) InputStream(java.io.InputStream) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) Test(org.junit.Test)

Example 60 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project coinbase-bitmonet-sdk by coinbase.

the class HTTPUtils method makeHttpPostWithJSONRequest.

public static HttpResponse makeHttpPostWithJSONRequest(String path, String json) {
    try {
        HttpPost httpPost = new HttpPost(path);
        httpPost.setEntity(new StringEntity(json));
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        return new DefaultHttpClient().execute(httpPost);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

ClientProtocolException (org.apache.http.client.ClientProtocolException)92 IOException (java.io.IOException)80 HttpResponse (org.apache.http.HttpResponse)49 HttpPost (org.apache.http.client.methods.HttpPost)37 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)30 HttpClient (org.apache.http.client.HttpClient)29 HttpGet (org.apache.http.client.methods.HttpGet)25 HttpEntity (org.apache.http.HttpEntity)23 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)20 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)19 InputStreamReader (java.io.InputStreamReader)18 ArrayList (java.util.ArrayList)18 UnsupportedEncodingException (java.io.UnsupportedEncodingException)17 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 BufferedReader (java.io.BufferedReader)13 InputStream (java.io.InputStream)13 NameValuePair (org.apache.http.NameValuePair)13 StringEntity (org.apache.http.entity.StringEntity)12 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)11 URI (java.net.URI)10