作者:吉翠芙_899 | 来源:互联网 | 2023-09-08 16:30
不知道为啥下面两种方式都无法获取参数值。指定Content-Type为applicationjson,发送json数据。˂br糟糕的诺言法1httpPost.setHeader;h
168飞艇6种不亏钱的方法
我不知道为什么不能用以下两种方法获取参数值。
将Content-Type指定为application/json以发送json数据。
htppost.setheader (' content-type ',' application/json; charset=utf-8 '; htppost.set entity (new string entity (params.tojsonstring ),StandardCharsets.UTF_8) );
htppost.setheader (' content-type ',' application/json; charset=utf-8 '; listnamevaluepairparamslist=new ArrayList (; Params.foreach((key,value )Paramslist.add ) newbasicnamevaluepair (key,) string (value ) ) ); ); htppost.set entity (newurlencodedformentity (params list,标准charsets.utf _8) ); 然后,尝试将Content-Type指定为application/x-www-form-urlencoded。 发送连接的数据,接口用实际参数名接收。 这样就可以获取参数值。 paramsStr=new StringBuilder (; Params.Foreach((key,value )- {try { paramsStr.append )甜美的紫菜encoder.encode(key,' UTF-8 ' ) ) ) )=}; ); htppost.setheader (' content-type ',' application/x-www-form-urlencoded; charset=utf-8 '; http post.set entity (new string entity (params str.substring (0,paramsStr.length )- 1 ),标准charsets.utf _ 8
package com.ieslab.wechatapi; import com.Alibaba.fast JSON.JSON object; importorg.Apache.http.http entity; importorg.Apache.http.namevaluepair; importorg.Apache.http.client.config.request config; importorg.Apache.http.client.entity.urlencodedformentity; importorg.Apache.http.client.methods.closeablehttpresponse; importorg.Apache.http.client.methods.http get; importorg.Apache.http.client.methods.http post; importorg.Apache.http.entity.string entity; importorg.Apache.http.impl.client.closeablehttpclient; importorg.Apache.http.impl.client.http clients; importorg.Apache.http.message.basicnamevaluepair; importorg.Apache.http.util.entity utils; import java.io.*; import java.net.*; import Java.nio.charset.standard charsets; import java.util.ArrayList; import java.util.List; /** * @description: Http请求工具类* @ author : Yiwen Li * @ create :2021-04-0809 :32 * */publicclasshttprequestutil (publicstaticvoidmain (string曾经的书包args ) throws幽默的心情(string URL=' http://localhost 33608000 ) parammap.put(date,) 2021-03-08 ); //stringresult=httprequest(URL,' GET ',paramMap ); stringresult=httprequest(URL,' POST ',paramMap ); kdwl=JSON object.parse object (result; system.out.println(11; }/* * *发送http请求* * @param requestUrl请求地址* @param params请求参数* @param requestMethod请求方式POST/GET */publicstaticstringhttprequest (stringrequesturl,JSONObject params,String requestMethod ) { String result=null; stringbuilderparamsstr=new stringbuilder (; Params.Foreach((key,value )- { try { paramsStr.append )甜美的紫菜encoder.encode(key,' UTF-8 ' ) ) ) )=(); if(post ).equals(requestmethod ) (requestcOnfig=request config.custom ).setsockettimeout (30 * ) )。 http post http post=new http post (request URL; htppost.set config (request config ); htppost.setheader (' content-type ',' application/x-www-form-urlencoded; charset=utf-8 '; http post.set entity (new string entity (params str.substring (0,paramsStr.length )- 1 ), StandardCharsets.UTF_8)是try (closeablehttpclienthttpclient=http clients.custom ().setdefaultrequestconfig (requequesttconfig ) coseablehttprespOnseresponse=http client.execute (http post ) ) httpentityentity=response.get entity ); result=entity utils.tostring (entity,' UTF-8 ' ); } catch (幽默的心情e ) { e.printStackTrace ); }elseif('get'.equals )请求方法) stringrequesturlwithparam=请求URL (?) Paramsstr.substring(0,paramsStr.length ) (- 1 ); http get http get=new http get (requesturlwithparam; closeablehttpclienthttpclient=http clients.create default (; coseablehttprespOnsehttpresponse=http client.execute (http get ) ) httpentity=httpresponse.get entity ); result=entity utils.tostring (http entity,' UTF-8 ' ); } catch (幽默的心情e ) { e.printStackTrace ); } }返回结果; }