JAVA调⽤HTTP接⼝POST或GET实现⽅式
HTTP是⼀个客户端和服务器端请求和应答的标准(TCP),客户端是终端⽤户,服务器端是⽹站。通过使⽤Web浏览器、⽹络爬⾍或者其它的⼯具,客户端发起⼀个到服务器上指定端⼝(默认端⼝为80)的HTTP请求。
具体POST或GET实现代码如下:
db.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apachemons.httpclient.DefaultHttpMethodRetryHandler;
import org.apachemons.httpclient.HttpClient;
import org.apachemons.httpclient.HttpException;
import org.hods.GetMethod;
import org.hods.PostMethod;
import org.apachemons.httpclient.params.HttpMethodParams;
public class HttpConnectUtil {
private static String DUOSHUO_SHORTNAME = "yoodb";//多说短域名 ****.yoodb.****
private static String DUOSHUO_SECRET = "xxxxxxxxxxxxxxxxx";//多说秘钥
/**
* get⽅式
* @param url
* @db
* @return
*/
public static String getHttp(String url) {
String responseMsg = "";
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = ResponseBodyAsStream();
int len = 0;
byte[] buf = new byte[1024];
while((ad(buf))!=-1){
out.write(buf, 0, len);
}
responseMsg = String("UTF-8");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放连接
}
return responseMsg;
}
/**
* post⽅式
* @param url
* @param code
* @param type
* @db
* @return
*/
public static String postHttp(String url,String code,String type) {
String responseMsg = "";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.addParameter(type, code);
postMethod.addParameter("client_id", DUOSHUO_SHORTNAME);
postMethod.addParameter("client_secret", DUOSHUO_SECRET);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = ResponseBodyAsStream();
int len = 0;
byte[] buf = new byte[1024];
while((ad(buf))!=-1){
out.write(buf, 0, len);
}
responseMsg = String("UTF-8");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return responseMsg;
}
}
1、下⾯说⼀下多说单点登录(SSO)获取access_token访问多说API的凭证。
多说单点登录(SSO),授权结束后跳转回在sso中设置的login地址,注意这时候的URL带上了code参数,通过code获取access_token访问多说API的凭证,具体实现代码如下:
public Map<String, String> getUserToken(String code){
String url = "api.duoshuo/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;
}
返回参数是⼀个JSON串,包含user_id和access_token,user_id是该⽤户在多说的ID,access_token是访问多说API的凭证,处理成Map集合⽅便使⽤。
浏览器json格式化2、如果获取多说的⽤户信息,根据上⼀步获得的多说⽤户user_id来获取⽤户的具体信息,详情代码如下:
public Map getProfiletMap(String userId){
String url = "api.duoshuo/users/profile.json?user_id="+userId;
String response = Http(url);
response = placeAll("\\\\", "");
System.out.println(response);
Gson gson = new Gson();
Map profile = gson.fromJson(response, Map.class);
return profile;
}
上述使⽤了Google处理json数据的jar,如果对Gson处理json数据的⽅式不很了解,参考地址:
返回的数据格式如下:
{
"response": {
"user_id": "13504206",
"name": "伤了⼼",
"url": "t.qq/wdg1115024292",
"avatar_url": "q.qlogo/qqapp/100229475/F007A1729D7BCC84C106D6E4F2ECC936/100",
"threads": 0,
"comments": 0,
"social_uid": {
"qq": "F007A1729D7BCC84C106D6E4F2ECC936"
},
"post_votes": "0",
"connected_services": {
"qqt": {
"name": "伤了⼼",
"email": null,
"avatar_url": "app.qlogo/mbloghead/8a59ee1565781d099f3a/50",
"url": "t.qq/wdg1115024292",
"description": "没劲",
"service_name": "qqt"
},
"qzone": {
"name": "?郁闷⼩佈?",
"avatar_url": "q.qlogo/qqapp/100229475/F007A1729D7BCC84C106D6E4F2ECC936/100",
"service_name": "qzone"
}
}
},
"code": 0
}
获取的⽤户信息不⽅便查看,建议格式化⼀下,Json校验或格式化地址:,返回数据参数说明:code int ⼀定返回
结果码。0为成功。失败时为错误码。
errorMessage strin
错误消息。当code不为0时,返回错误消息。
response object
json对象。当code为0时,返回请求到的json对象。
来源:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论