HttpClient send without parameter post data

Source: Internet
Author: User

Two questions:1, httpclient How to send a no no matter what the number of post data? 2, WebProject How to receive a non-reference post?
causes:A problem was encountered in development today (2014.11.10). Interfaces provided by the interface provider are required to send data using post. Thought it was a simple thing? Direct post is not the past. However, the interface provided is not for any number of parameters. This is not an interface like this http://api.dutycode.com/data/parm=xxx, but a http://api.dutycode.com/data.     This address directly receives the post data. Say, at that moment heart-broken, did not touch ah ...

。 However, there is always a way out. Since there is such an interface to receive data, you must be able to send so
Workaround: Very easy to implement code such as the following:

Public StaticvoidMain (string[] args)throwsException { HttpClient client = httpclients. Createdefault ();            HttpPost post =New HttpPost ( "Http://127.0.0.1/report/testPost");                         //Assemble a json string. Used to send Jsonobject jsonobj =New jsonobject (); Jsonobj.put ("website" , "Http://www.dutycode.com");Jsonobj.put ("Email" , "[email protected]" );            stringentity entity =New stringentity (jsonobj.tojsonstring ()); entity.setcontentencoding ("UTF-8" );Entity.setcontenttype ("Application/json" ); //Set as JSON data             post.setentity (entity);            HttpResponse response = Client.execute (post);            httpentity resentity = response.getentity (); String res = entityutils. toString (resentity);             System. out . println (res);       }

2 WebProject How to receive a non-reference post?
Since can send, then have to find a way to achieve service end Ah, otherwise how talent forget.
So test code: (note, implemented using the company's internal framework.) But the rationale is the same)
@Path ("Testpost") Public actionresult getpost () throwsexception{StringBuilder sb = NewStringBuilder ( ); InputStream is =getrequest (). getInputStream ();Bufferedinputstream bis =New Bufferedinputstream (IS);             byte [] buffer = New byte[1024x768];            int read = 0;              while ((read=bis.read (buffer))! =-1) { Sb.append (New String (buffer, 0, read, "UTF-8"));            }            System. out . println (Sb.tostring ());             return OutputStream ( "{msg:success}");}

The principle is very easy. Get direct access to all of the data streams that come to post

The above two combined together to test, the results such as the following: the first paragraph of the code to return the results:
{msg:success}
The second piece of code returns the result:
{"Email": "[email protected]", "website": "Http://www.dutycode.com"}


W=80&m=5&text=http://www.dutycode.com/post-76.html "align=" left "style=" border:0px "> All rights Reserved:" Climbing the Snail "and" httpclient Send no-reference post data
This address: http://www.dutycode.com/post-76.html
Unless noted. The article is "snail Crawl" original, welcome reprint! Address reproduced this article, please specify, thank you

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

HttpClient send without parameter post data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.