HTTP is a client and server-side request and response Standard (TCP), the client is the end user, and the server side is the Web site. By using a Web browser, crawler, or other tool, the client initiates an HTTP request to the specified port on the server (the default port is 80).
The specific post or get implementation code is as follows:
Package com.yoodb.util;import java.io.bytearrayoutputstream;import java.io.ioexception;import java.io.InputStream;import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;import org.apache.commons.httpclient.httpclient;import org.apache.commons.httpclient.httpexception;import org.apache.commons.httpclient.methods.getmethod;import org.apache.commons.httpclient.methods.postmethod; import org.apache.commons.httpclient.params.httpmethodparams;public class httpconnectutil {private static string duoshuo_shortname = "Yoodb";//More short domain name ****.yoodb.**** private static string duoshuo_secret = "xxxxxxxxxxxxxxxxx";//Say secret key/** * Get mode * @param url * @author www.yoodb.com * @return */public static string gethttp (String url) {String responseMsg = ""; httpclient httpclient = New httpclient (); Getmethod getmethod = new getmethod (URL); Getmethod.getparams (). Setparameter ( Httpmethodparams.retry_handler,new defaulthttpmethodretryhandler ()); Try {httpclient.executemethod ( GetMethod); Bytearrayoutputstream out = new bytearrayoutputstream ();inputstream in = Getmethod.getresponsebodyasstream (); int len = 0;byte[] buf = new byte[1024] ; while ((Len=in.read (BUF))!=-1) {out.write (Buf, 0, len);} Responsemsg = out.tostring ("UTF-8");} catch (httpexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();} finally {//Release Connection Getmethod.releaseconnection ();} Return responsemsg;} /** * post Way * @param url * @param code * @param type * @author www.yoodb.com * @return */public static string posthttP (string url,string code,string type) {String responseMsg = ""; Httpclient httpclient = new httpclient (); Httpclient.getparams (). SetContentCharset ("GBK"); Postmethod postmethod = new postmethod (URL);p ostmethod.addparameter (Type, code); Postmethod.addparameter ("client_id", duoshuo_shortname);p ostmethod.addparameter ("Client_secret", Duoshuo_secret); Try {httpclient.executemethod (Postmethod); Bytearrayoutputstream out = new bytearrayoutputstream ();inputstream in = Postmethod.getresponsebodyasstream (); int len = 0;byte[] buf = new byte[1024 ];while ((Len=in.read (BUF))!=-1) {out.write (Buf, 0, len);} Responsemsg = out.tostring ("UTF-8");} catch (httpexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();} finally {postmethod.releaseconnection ();} RetUrn responsemsg;}}
1, the following say more about Single sign-on (SSO) Get Access_token access to the multi-said API credentials.
Say Single Sign-on (SSO), after authorization to skip back to the login address set in SSO, note that this time the URL with the code parameter, through code to obtain Access_token access to the multi-said API credentials, the implementation code is as follows:
Public map<string, string> Getusertoken (String code) {String url = "Http://api.duoshuo.com/oauth2/access_token"; String response = httpconnectutil.posthttp (URL, code, "code"); SYSTEM.OUT.PRINTLN (response); Gson Gson = new Gson (); map<string, string> retmap = Gson.fromjson (response,new typetoken<map<string, String>> () {}.getType ()); return retmap;}
The return parameter is a JSON string that contains user_id and access_token,user_id is the user in many said Id,access_token is access to the multi-API credentials, processing into a map collection for ease of use.
2, if you get more user information, according to the previous step to obtain more users user_id to obtain the user's specific information, the details of the code as follows:
Public Map Getprofiletmap (string userId) {string url = "Http://api.duoshuo.com/users/profile.json?user_id=" +userid; String response = httpconnectutil.gethttp (URL); response = Response.replaceall ("\\\\", ""); SYSTEM.OUT.PRINTLN (response); Gson Gson = new Gson (); Map profile = Gson.fromjson (response, Map.class);
The above uses the jar of Google processing JSON data, if the Gson processing JSON data is not very understanding, reference address:http://www.yoodb.com/article/display/1033
The returned data format is as follows:
{ "Response": { "user_id": " 13504206 ", " name ": " Wounded Heart ", "url": "http://t.qq.com/wdg1115024292", "Avatar_url": "http://q.qlogo.cn/qqapp/100229475/F007A1729D7BCC84C106D6E4F2ECC936/100", "Threads": 0, "comments": 0, "Social_uid": { "QQ": "f007a1729d7bcc84c106d6e4f2ecc936" }, "post_votes": "0", "Connected_services": { &nBSP; " QQT ": { " Name ": " broke the Heart ", "Email": null, "Avatar_url": "http://app.qlogo.cn/mbloghead/8a59ee1565781d099f3a/50", "url": "http://t.qq.com/ wdg1115024292 ", "description": "boring", "service_name": "QQT" }, "Qzone": { "name": "?", "Avatar_url": "http://q.qlogo.cn/qqapp/ 100229475/f007a1729d7bcc84c106d6e4f2ecc936/100 ", "service_name": "Qzone" } } }, "code": 0}
The user information obtained is not convenient to view, it is recommended to format, JSON check or format address:Http://www.yoodb.com/toJson, return data parameter description:
Code INT must return
The result code. 0 for success. Failure is an error code.
ErrorMessage Strin
The error message. When code is not 0 o'clock, an error message is returned.
Response Object
A JSON object. When code is 0 o'clock, the requested JSON object is returned.
Source: http://www.yoodb.com/article/display/1034
JAVA invoke HTTP interface post or get implementation mode