Search in sources :

Example 51 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project CodeUtils by boredream.

the class LeanCloudHttpUtils method postFile.

public static String postFile(String url, File file) throws Exception {
    // LeanCloud上传限制, 最多1秒1个
    Thread.sleep(1000);
    HashMap<String, String> map = getHeaderMap();
    map.put("Content-Type", new MimetypesFileTypeMap().getContentType(file));
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.addRequestProperty("Content-Type", new MimetypesFileTypeMap().getContentType(file));
    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    int len;
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(file);
    while ((len = fis.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    out.close();
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
    String responseCharset = parseCharset(contentTypeHeader);
    byte[] bytes = entityToBytes(response.getEntity());
    String responseContent = new String(bytes, responseCharset);
    return responseContent;
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 52 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project CodeUtils by boredream.

the class BmobHttpUtils method getOrPostString.

private static String getOrPostString(int method, String url, Map<String, String> postParams) throws Exception {
    HashMap<String, String> map = new HashMap<String, String>();
    // bmob header
    map.put("X-Bmob-Application-Id", APP_ID);
    map.put("X-Bmob-REST-API-Key", REST_API_KEY);
    map.put("Content-Type", "application/json");
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, method, postParams);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
    String responseCharset = parseCharset(contentTypeHeader);
    byte[] bytes = entityToBytes(response.getEntity());
    String responseContent = new String(bytes, responseCharset);
    return responseContent;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 53 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project CodeUtils by boredream.

the class LeanCloudHttpUtils method postBean.

public static String postBean(Object object) throws Exception {
    String url = "https://api.leancloud.cn/1.1/classes/";
    url += object.getClass().getSimpleName();
    HashMap<String, String> map = getHeaderMap();
    map.put("Content-Type", HEADER_CONTENT_TYPE);
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.addRequestProperty("Content-Type", HEADER_CONTENT_TYPE);
    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    out.write(new Gson().toJson(object).getBytes());
    out.close();
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
    String responseCharset = parseCharset(contentTypeHeader);
    byte[] bytes = entityToBytes(response.getEntity());
    return new String(bytes, responseCharset);
}
Also used : Gson(com.google.gson.Gson) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 54 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project gocd by gocd.

the class RemoteRegistrationRequesterTest method shouldPassAllParametersToPostForRegistrationOfNonElasticAgent.

@Test
public void shouldPassAllParametersToPostForRegistrationOfNonElasticAgent() throws IOException, ClassNotFoundException {
    String url = "http://cruise.com/go";
    GoAgentServerHttpClient httpClient = mock(GoAgentServerHttpClient.class);
    final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    final ProtocolVersion protocolVersion = new ProtocolVersion("https", 1, 2);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(protocolVersion, HttpStatus.OK.value(), null));
    when(response.getEntity()).thenReturn(new StringEntity(RegistrationJSONizer.toJson(createRegistration())));
    when(httpClient.execute(argThat(isA(HttpUriRequest.class)))).thenReturn(response);
    final DefaultAgentRegistry defaultAgentRegistry = new DefaultAgentRegistry();
    Properties properties = new Properties();
    properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_KEY, "t0ps3cret");
    properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_RESOURCES, "linux, java");
    properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_ENVIRONMENTS, "uat, staging");
    properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_HOSTNAME, "agent01.example.com");
    remoteRegistryRequester(url, httpClient, defaultAgentRegistry, 200).requestRegistration("cruise.com", new AgentAutoRegistrationPropertiesImpl(null, properties));
    verify(httpClient).execute(argThat(hasAllParams(defaultAgentRegistry.uuid(), "", "")));
}
Also used : StringEntity(org.apache.http.entity.StringEntity) AgentAutoRegistrationPropertiesImpl(com.thoughtworks.go.agent.AgentAutoRegistrationPropertiesImpl) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) GoAgentServerHttpClient(com.thoughtworks.go.agent.common.ssl.GoAgentServerHttpClient) ProtocolVersion(org.apache.http.ProtocolVersion) DefaultAgentRegistry(com.thoughtworks.go.config.DefaultAgentRegistry) Properties(java.util.Properties) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 55 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project gocd by gocd.

the class SslInfrastructureServiceTest method shouldGetTokenFromServerIfOneNotExist.

@Test
public void shouldGetTokenFromServerIfOneNotExist() throws Exception {
    final ArgumentCaptor<HttpRequestBase> httpRequestBaseArgumentCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
    tokenService.delete();
    when(agentRegistry.uuid()).thenReturn("some-uuid");
    when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("https", 1, 2), 200, null));
    when(httpResponse.getEntity()).thenReturn(new StringEntity("token-from-server"));
    when(httpClient.execute(httpRequestBaseArgumentCaptor.capture())).thenReturn(httpResponse);
    sslInfrastructureService.getTokenIfNecessary();
    verify(agentRegistry).storeTokenToDisk("token-from-server");
    final HttpRequestBase httpRequestBase = httpRequestBaseArgumentCaptor.getValue();
    final List<NameValuePair> nameValuePairs = URLEncodedUtils.parse(httpRequestBase.getURI(), StandardCharsets.UTF_8);
    assertThat(findParam(nameValuePairs, "uuid").getValue(), is("some-uuid"));
}
Also used : StringEntity(org.apache.http.entity.StringEntity) NameValuePair(org.apache.http.NameValuePair) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Aggregations

ProtocolVersion (org.apache.http.ProtocolVersion)113 BasicStatusLine (org.apache.http.message.BasicStatusLine)54 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)43 StatusLine (org.apache.http.StatusLine)42 Test (org.junit.Test)33 Header (org.apache.http.Header)26 HttpEntity (org.apache.http.HttpEntity)26 HttpResponse (org.apache.http.HttpResponse)22 StringEntity (org.apache.http.entity.StringEntity)20 URL (java.net.URL)16 BasicHeader (org.apache.http.message.BasicHeader)16 IOException (java.io.IOException)15 HttpURLConnection (java.net.HttpURLConnection)15 List (java.util.List)15 HashMap (java.util.HashMap)14 HttpHost (org.apache.http.HttpHost)12 ParseException (org.apache.http.ParseException)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11 MockHttpStack (com.android.volley.mock.MockHttpStack)10 HttpRequest (org.apache.http.HttpRequest)10