Recently in the development of a project, need to involve the use of HTTP request to send relatively large data, study for a long time, encounter problems, solve problems, here to share to everyone
1. Due to the large amount of data, the Post method is used to transmit data (post theoretically does not limit the size of the data, but different servers will have the corresponding default settings limit data size)
2. Using JSON-formatted data for project needs
code example:
JAVA version, using Apache's commons-httpclient package to send HTTP requests, code for reference only, send request method can be modified according to their own needs
Importorg.apache.commons.httpclient.*;//using Apache Commons httpclientPrivate Static FinalString Application_json = "Application/json;charset=uft-8"; Private Static FinalString Content_type_text_json = "Text/json"; /*** Send json* using HTTP Post@paramurl*@paramJSON to choose your favorite JSON package format data *@throwsException*/ Public Static voidHttppostwithjson (string URL, string json)throwsException {//UTF-8 encoding of JSON for transfer of ChineseString Encoderjson = Urlencoder.encode (JSON, "UTF-8"); HttpClient HttpClient=NewHttpClient (); Postmethod Method=Newpostmethod (URL); Requestentity requestentity=Newstringrequestentity (Encoderjson); Method.setrequestentity (requestentity); Method.addrequestheader ("Content-type", Application_json); intresult =Httpclient.executemethod (method); System.out.println ("Response Status code:" +result); System.out.println ("Response Body:" ); System.out.println (Method.getresponsebodyasstring ()); Method.releaseconnection ();}
PHP version, using curl, here to note: Curl uses post to send a request when the data is greater than 1024 bytes, it automatically joins the expect:100-continue in the request header, causing the request to hang and not get the response from the destination server.
Workaround: Manually set expect on the request header before sending the request: null to cancel expect:100-continue the following is the code example:
<?PHP/** * PHP Send JSON Object data * * @param $url Request URL * @param $jsonStr the JSON string sent * @return array*/functionHttp_post_json ($url,$jsonStr){ $ch=Curl_init (); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_postfields,$jsonStr); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_httpheader,Array( ' Content-type:application/json; Charset=utf-8 ', ' content-length: '.strlen($jsonStr),
' Expect: '); $response= Curl_exec ($ch); $httpCode= Curl_getinfo ($ch,Curlinfo_http_code); return Array($httpCode,$response);}$arr=Array( ' Service ' = ' s_scenic_info ', ' count ' = ten, ' batchno ' = ' JQ201609011230590001 ', ' flag ' =& Gt ' 11111 ', ' data ' =Array( Array( ' DisplayName ' = ' 15980851200 ', ' authenticket ' = ' 8b11d343d766d4af88d6b8746ec4e786 ', ' Authenuserid ' = ' 1120d8d6ea4a4850b65d0faa40d6dffb ', ' timestamp ' = ' 20151009195525 ' ),Array( ' DisplayName ' = ' 15980851200 ', ' authenticket ' = ' 8b11d343d766d4af88d6b8746ec4e786 ', ' Authenuserid ' = ' 1120d8d6ea4a4850b65d0faa40d6dffb ', ' timestamp ' = ' 20151009195525 ' ) ) );$url= "Http://wangming.hk1.ngrok.cc/tmdata/Dispatcher.do";$jsonStr= Json_encode ($arr);Echo($jsonStr);List($returnCode,$returnContent) = Http_post_json ($url,$jsonStr);Echo($returnContent)?>
Expect:100-continue
When using the Libcurl post method, if the post data size is greater than 1024 bytes, Libcurl does not send the POST request directly, but is divided into two steps to execute the request:
1, send a request, the request header contains a expect:100-continue field, to ask the server is willing to accept the data
2. After receiving an answer from the 100-continue returned from the server, it will actually initiate the POST request and send the data to the server.
For the field "100-continue", the RFC document (http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html# sec8.2.3) explains this: it allows the client to determine whether the server is willing to receive the data before sending the request data, and if the server is willing to receive it, the client will actually send the data, because if the client sends the request data directly, but the server rejects the request, this behavior can lead to significant resource overhead. So in order to avoid this, Libcurl takes this approach when sending a POST request that is larger than 1024 bytes, but in contrast, it causes a delay in the request, and not all servers will correctly handle and answer "100-continue". such as LIGHTTPD, will return 417 "expectation Failed", resulting in a request logic error.
If you are sure that the server does not reject more than 1024 bytes of post requests, you can avoid using this method and you can prevent the two side effects mentioned above
Reference:
Http://www.laruence.com/2011/01/20/1840.html
HTTP POST sends JSON format data (resolves expect:100-continue issues)