HttpClient关闭控制台的DEBUG输出-亲测可用2021

亲测可用

网上一堆关于HttpClient关闭控制台的DEBUG输出 、log4j去除httpclient中contend&header的日志信息的方法的博客,但大多数都不起作用

没关debug日志之前是这样的。。。1-1

关之后的效果,完美1-2

 

其他博客提到的在log4j.properties增加

### httpClient, wire->header
log4j.logger.httpclient=error     
log4j.logger.httpclient.wire.header=ERROR     
log4j.logger.org.apache.commons.httpclient=ERROR

完美的完全没用。。。

真正的解决办法:

如果你的工程是非父子结构的则

在/Users/qa/Desktop/2021/code/cloud/cloud-api/src/main/resources/   下新建logback.xml

logback.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <!-- definition of appender STDOUT -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>

    <root level="ERROR">
        <!-- appender referenced after it is defined -->
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

 

如果你的工程是父子结构的则

在/Users/qa/Desktop/code//src/main/resources也就是resources

如果是SpringBoot框架,则在/Users/qa/Desktop/xx/javaAuto/xx-api(server层,非app层)/src/main/resources/logback.xml

下添加logback.xml 。记得不是在src下,看有的博客那么写的。。。

配置如下,效果见上图1-2

<configuration debug="false">
    <!--以下三行一定注释掉,否则还会打印部分debug日志,o.apache.commons.httpclient.HttpClient-->
    <!--<logger name="org.apache" level="DEBUG" />-->
    <!--<logger name="org.apache.http.wire" level="DEBUG" />-->
    <!--<logger name="org.apache.http.headers" level="INFO" />-->

    <property name="CONSOLE_LOG_PATTERN"
              value="%date{yyyy-MM-dd HH:mm:ss}  %highlight(%-5level) %magenta(%-4relative) --- [%yellow(%15.15thread)] %cyan(%-40.40logger{39}) : %msg%n"/>


    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <root level="ERROR">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

 

如果不注释以下三行,效果如下

<!--<logger name="org.apache" level="DEBUG" />-->
<!--<logger name="org.apache.http.wire" level="DEBUG" />-->
<!--<logger name="org.apache.http.headers" level="INFO" />-->

 

 

 

20:35:39.687 [main] DEBUG httpclient.wire.header - >> "GET /search/query?&appScene=detail_page&aisdk_appKey=xx&test_ab=c&young=0&appCaller=xx&zx=0 HTTP/1.1[\r][\n]"
20:35:39.687 [main] DEBUG org.apache.commons.httpclient.HttpMethodBase - Adding Host request header
20:35:39.688 [main] DEBUG httpclient.wire.header - >> "Content-type: application/json[\r][\n]"
20:35:39.688 [main] DEBUG httpclient.wire.header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
20:35:39.688 [main] DEBUG httpclient.wire.header - >> "Host: pre-xx-search.xx.com[\r][\n]"
20:35:39.688 [main] DEBUG httpclient.wire.header - >> "[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "HTTP/1.1 200 [\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "HTTP/1.1 200 [\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Date: Sun, 22 Mar 2020 12:35:40 GMT[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Content-Type: application/json[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Content-Length: 61352[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Connection: keep-alive[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Vary: Accept-Encoding[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Vary: Accept-Encoding[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "X-Content-Type-Options: nosniff[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "X-XSS-Protection: 1; mode=block[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Cache-Control: no-cache, no-store, max-age=0, must-revalidate[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Pragma: no-cache[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Expires: 0[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "X-Frame-Options: DENY[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Strict-Transport-Security: max-age=31536000 ; includeSubDomains[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Set-Cookie: XSRF-TOKEN=c74ed2f8-d16d-405a-bcc2-b4a9956128ef; Path=/; HttpOnly[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "X-Application-Context: xx-search:staging:7001[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Server: Tengine/Aserver[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "Timing-Allow-Origin: *[\r][\n]"
20:35:40.086 [main] DEBUG httpclient.wire.header - << "s-rt: 383[\r][\n]"
20:35:40.087 [main] DEBUG httpclient.wire.header - << "[\r][\n]"
20:35:40.087 [main] DEBUG org.apache.commons.httpclient.cookie.CookieSpec - Unrecognized cookie attribute: name=HttpOnly, value=null
20:35:40.087 [main] DEBUG org.apache.commons.httpclient.HttpMethodBase - Cookie accepted: "$Version=0; XSRF-TOKEN=c74ed2f8-d16d-405a-bcc2-b4a9956128ef; $Path=/"
20:35:40.087 [main] DEBUG org.apache.commons.httpclient.HttpMethodBase - Buffering response body
20:35:40.087 [main] DEBUG httpclient.wire.content - << "{"level":-1,"nodes":[{"data":{"pz":30,"isEnd":0,"total":67,"typeVersion":"B","engine":"xx","pg":2,"action":{"report":{"trackInfo":{"searchtab":"0","engine":"xx","k":"[0xe8][0xb6][0x8a][0xe6][0x88][0x98][0xe7][0x94][0xb5][0xe5][0xbd][0xb1]"},"pageName":"page_searchdetailshowword","spmAB":"a2h0c.8167947"}},"status":0},"level":0,"more":true,"type":0,"nodes":[{"data":{"logCate":"programset","docId":"126270_31806_33526_49614_127668_11332","docSource":10,"itemLog":"wide_flag~1$doc_source~10$eng_source~6$sp_id~1153463830127312896","cateId":7},"level":1,"more":false,"type":1004,"nodes":[{"data":{"logCate":"programset","total":67,"cateId":7},"level":2,"more":false,"type":1006,"nodes":[{"data":{"sourceId":14,"realShowId":"d4b3e3bcb8ff11e0bf93","releaseDate":"1980","docId":"126270","posterDTO":{"iconCorner":{"tagText":"VIP","tagType":3},"vThumbUrl":"http://r1.ykimg.com/0516000055DC388A67BC3C62C4051C78?x-oss-process=image/format,webp","reputation":6.8,"action":{"type":"JUMP_TO_NATIVE","report":{"spm":"a2h0c.8167947.xx","arg1":"xx","scm":"20140669.search.rprogramset.show_d4b3e3bcb8ff11e0bf93"},"value":"xx://play?source=search&showid=d4b3e3bcb8ff11e0bf93&politicsSensitive=false&searchKey=%E8%B6%8A%E6%88%98%E7%94%B5%E5%BD%B1"}},"completed":1,"sourceImg":"http://r1.ykimg.com/051000005A5D5E3CADC0AE1B09011DC4","titleDTO":{"displayName":"[0xe4][0xba][0xa1][0xe5][0x91][0xbd][0xe5][0xa4][0xa7][0xe6][0x8d][0x95][0xe5][0xa4][0xb4]","action":{"type":"JUMP_TO_NATIVE","report":{"spm":"a2h0c.8167947.xx.title_1","arg1":"xx","scm":"20140669.search.rprogramset.show_d4b3e3bcb8ff11e0bf93"},"value":"xx://play?source=search&showid=d4b3e3bcb8ff11e0bf93&politicsSensitive=false&searchKey=%E8%B6%8A%E6%88%98%E7%94%B5%E5%BD%B1"}},"cats":"[0xe7][0x94][0xb5][0xe5][0xbd][0xb1]","showId":"d4b3e3bcb8ff11e0bf93","tempTitle":"[0xe4][0xba][0xa1][0xe5][0x91][0xbd][0xe5][0xa4][0xa7][0xe6][0x8d][0x95][0xe5][0xa4][0xb4]","cateId":2,"action":{"type":"JUMP_TO_NATIVE","report":{"trackInfo":{"object_type":101,"group_num":31,"object_title":"[0xe4][0xba][0xa1][0xe5][0x91][0xbd][0xe5][0xa4][0xa7][0xe6][0x8d][0x95][0xe5][0xa4][0xb4]","group_type":99&showid=d4b3e3bcb8ff11e0bf93&politicsSensitive=false&searchKey=%E8%B6%8A%E6%88%98%E7%94%B5%E5%BD%B1"},"showIdValid":-1,"sourceName":"[0xe4][0xbc][0x98][0xe9][0x85][0xb7]","thumbUrl":"https://r1.ykimg.com/050C000055DC37D567BC3C612703A469?x-oss-process=image/format,webp","programId":"126270","desc":"[0xe8][0xb5][0x8f][0xe9][0x87][0x91][0xe7][0x8c][0x8e][0xe4][0xba][0xba][0xe7][0x9a][0x84][0xe6][0x97][0xa0][0xe5][0xa5][0x88][0xe5][0x92][0x8c][0xe5][0x89][0xb2][0xe8][0x88][0x8d]","notice":"[0xe5][0xaf][0xbc][0xe6][0xbc][0x94] [0xe5][0xb7][0xb4][0xe5][0x85][0xb9][0xc2][0xb7][0xe5][0xba][0x93][0xe5][0x88][0xa9][0xe5][0x85][0x8b] / [0xe4][0xb8][0xbb][0xe6][0xbc][0x94] [0xe5][0x8f][0xb2][0xe8][0x92][0x82][0xe5][0xa4][0xab][0xc2][0xb7][0xe9][0xba][0xa6][0xe5][0xa5][0x8e][0xe5][0x9b][0xa0] Tracey Walter Thomas Rosales Jr. [0xe5][0x8b][0x92][0xe7][0x93][0xa6][0xe5][0xb0][0x94][0xc2][0xb7][0xe5][0xb8][0x83][0xe5][0xb0][0x94][0xe9][0xa1][0xbf]"},"level":3,"more":false,"type":1501,"style":[{"type":0}],"id":0},{"data":{"realShowId":"cc082f52962411de83b1","releaseDate":"1988","docId":"31806","posterDTO":{"vThumbUrl":"http://r1.ykimg.com/0516000051EE362167583916BB068438?x-oss-process=image/format,webp","reputation":6.5,"action":{"type":"JUMP_TO_NATIVE","report":[0x8e][0xe7][0x8b][0xbc][0xe5][0x91][0xbc][0xe5][0x8f][0xab]21&vThumbUrl=http%3A%2F%2Fr1.ykimg.com%2F0516000051EE362167583916BB068438"}},"cats":"[0xe7][0x94][0xb5][0xe5][0xbd][0xb1]","showId":"cc082f52962411de83b1","tempTitle":"[0xe9][0x87][0x8e][0xe7][0x8b][0xbc][0xe5][0x91][0xbc][0xe5][0x8f][0xab]21","cateId":2[0x8b]3&vThumbUrl=http%3A%2F%2Fr1.ykimg.com%2F0516000053BCA6C967379F159805DC92"},"showIdValid":-1,"thumbUrl":"https://r1.ykimg.com/050C000053BCA6BE67379F113A04D82E?x-oss-process=image/format,webp","programId":"25421","desc":"","notice":"[0xe5][0xaf][0xbc][0xe6][0xbc][0x94] [0xe4][0xba][0x9a][0xe4][0xbc][0xa6][0xc2][0xb7][0xe8][0xaf][0xba][0xe7][0x91][0x9e][0xe6][0x96][0xaf] / [0xe4][0xb8][0xbb][0xe6][0xbc][0x94] Pita Liboro Rick Prieto Roland Harrah III [0xe6][0x9f][0xa5][0xe5][0x85][0x8b][0xc2][0xb7][0xe8][0xaf][0xba][0xe7][0x91][0x9e][0xe6][0x96][0xaf]"},"level":3,"more":false,"type":1501,"style":[{"type":0}],"id":0}],"style":[{"type":0}],"id":0}],"style":[{"type":0}],"id":0}],"style":[{"type":0}],"id":0}]}"
20:35:40.168 [main] DEBUG org.apache.commons.httpclient.HttpMethodBase - Should NOT close connection in response to directive: keep-alive
20:35:40.168 [main] DEBUG org.apache.commons.httpclient.HttpConnection - Releasing connection back to connection manager.

 

 

  • 11
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于Java中的HTTPClient请求multipart/form-data,你可以使用Apache HttpClient库来实现。下面是一个简单的示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.HttpClients; import java.io.File; import java.io.IOException; public class MultipartFormDataExample { public static void main(String[] args) { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://example.com/upload"); // 创建一个多部分实体构建器 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // 添加文本参数 builder.addTextBody("username", "John Doe"); // 添加文件参数 File file = new File("path/to/image.jpg"); builder.addBinaryBody("image", file, ContentType.APPLICATION_OCTET_STREAM, file.getName()); // 构建多部分实体 HttpEntity multipartEntity = builder.build(); // 将多部分实体设置为请求的实体 httpPost.setEntity(multipartEntity); try { // 执行请求 HttpResponse response = httpClient.execute(httpPost); // 处理响应 // ... } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述代码中,我们使用了Apache HttpClient库来进行HTTP请求。首先,我们创建一个`HttpClient`实例,并指定要进行POST请求的URL。然后,我们创建一个`MultipartEntityBuilder`实例,用于构建多部分实体。我们可以使用`addTextBody`方法添加文本参数,使用`addBinaryBody`方法添加文件参数。最后,我们通过调用`build`方法构建多部分实体,并将其设置为POST请求的实体。最后,我们执行请求并处理响应。 希望以上的示例代码能对你有所帮助。<span class="em">1</span> #### 引用[.reference_title] - *1* [c#实现HttpClient拼接multipart/form-data形式参数post提交数据](https://download.csdn.net/download/kgo00/12091747)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方狱兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值