有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

apache如何在使用httpClent java上传时取得进展

有人能告诉我如何上传文件并在其他课堂上使用吗

使用任何侦听器或包装器等

public static SusResponseDTO postRequest( String url, String payload, Map< String, String > requestHeaders ) {
    CloseableHttpClient httpClient = null;
    HttpPost httpPost = null;
    CloseableHttpResponse response = null;
    SusResponseDTO susResponseDTO = null;
    try {
        httpClient = HttpClients.createDefault();
        httpPost = new HttpPost( url );

        final StringEntity input = new StringEntity( payload, DEFAULT_ENCODING );
        input.setContentType( JSOM_MIME_TYPE );

        httpPost.setEntity( input );

        for ( final Entry< String, String > h : requestHeaders.entrySet() ) {
            httpPost.addHeader( h.getKey(), h.getValue() );
        }
        response = httpClient.execute( httpPost );

        if ( response.getStatusLine().getStatusCode() != 200 ) {
            throw new SusException( FAILED_HTTP_ERROR_CODE + response.getStatusLine().getStatusCode() );
        }
        for ( Header header : response.getAllHeaders() ) {
            if ( header.getName().equals( SERVER ) && header.getValue().contains( MASTER_SERVER ) ) {
                susResponseDTO = JsonUtils.jsonStreamToObject( response.getEntity().getContent(), SusResponseDTO.class );
                break;
            }
        }

        if ( susResponseDTO == null ) {
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString( entity, DEFAULT_ENCODING );
            Map< String, String > map = new HashMap<>();
            map = ( Map< String, String > ) JsonUtils.jsonToMap( responseString, map );
            susResponseDTO = JsonUtils.jsonToObject( map.get( RESPONSE_ENTITY ), SusResponseDTO.class );
        }
    } catch ( final IOException e ) {
        ExceptionLogger.logException( e, SuSClient.class );
        throw new SusException( e, SuSClient.class );
    } finally {
        IOUtils.safeClose( response );
        IOUtils.safeClose( httpClient );
    }
    return susResponseDTO;
}

共 (0) 个答案