http://www.java2000.net/p7714
http://blog.csdn.net/java2000_net/archive/2008/07/30/2736268.aspx
此文的版權歸JAVA世紀網(www.java2000.net)和CSDN(www.csdn.net)所有,轉載請保留此聲明、代碼註釋和原始鏈接
CSDN的 OpenAPI 提供了WebService 接口,可以使用一些現有的框架直接生成客戶端調用程序,比如axis。我這裏提供了另外一個簡單那的方法,直接使用URLConnection進行操作,並自行解析登錄的結果。
方法的說明
/**
* 檢查用戶名和密碼。
*
* @author 趙學慶,www.java2000.net
* @param username 用戶名
* @param password 密碼
* @return 如果成功,則返回true,失敗返回false
*/
public static boolean checkLogin(String username, String password)
準備數據:
OpenAPI裏面提供了GetUserProfile的方法
我們看一下協議裏面的請求部分的格式,SOAP1.2格式
http://forum.csdn.net/OpenApi/forumapi.asmx?op=GetUserProfile
POST /OpenApi/forumapi.asmx HTTP/1.1
Host: forum.csdn.net
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetUserProfile xmlns="http://www.csdn.net/">
<identity>
<username>string</username>
<password>string</password>
</identity>
<username>string</username>
</GetUserProfile>
</soap12:Body>
</soap12:Envelope>
此方法需要提供3個參數。
1 會員用戶名
2 會員的密碼
3 要查看人員的用戶名
比如你想看aaa的情況,你的用戶名和密碼是bbb/ccc,則調用爲
GetUserProfile(bbb,ccc,aaa);
我們使用中,可以讓aaa和bbb相等,也就是自己看自己。
我們看着部分代碼
// 以下構造提交用的web服務的內容
StringBuilder b = new StringBuilder();
b.append("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
b.append("<soap12:Envelope xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" ");
b.append("xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" ");
b.append("xmlns:soap12=/"http://www.w3.org/2003/05/soap-envelope/">");
b.append("<soap12:Body>");
// 這裏是調用的功能標識
b.append(" <GetUserProfile xmlns=/"http://www.csdn.net//">");
// 這裏是調用的用戶信息標識
b.append(" <identity>");
// 登錄用戶名
b.append(" <username>" + username + "</username>");
// 登錄密碼
b.append(" <password>" + password + "</password>");
b.append(" </identity>");
// 要查看的用戶名,和登錄用戶相同
b.append(" <username>" + username + "</username>");
b.append(" </GetUserProfile>");
b.append("</soap12:Body>");
b.append("</soap12:Envelope>");
數據的提交
直接使用URLConnection進行模擬的POST提交,並讀取提交的結果
// 提交數據,並獲取返回結果
String textXml = postPage("http://forum.csdn.net/OpenApi/forumapi.asmx", "forum.csdn.net",
null, b.toString())[1];
其中的postPage我將放在最後面的代碼列表裏面
返回結果的分析
我們看一下協議裏面的返回數據的格式,SOAP1.2格式
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetUserProfileResponse xmlns="http://www.csdn.net/">
<GetUserProfileResult>boolean</GetUserProfileResult>
<profile>
<point>int</point>
<techExpertPoint>int</techExpertPoint>
<topForums>
<TopForum>
<forumId>guid</forumId>
<expertPoint>int</expertPoint>
<rank>string</rank>
</TopForum>
<TopForum>
<forumId>guid</forumId>
<expertPoint>int</expertPoint>
<rank>string</rank>
</TopForum>
</topForums>
<nonTechExpertPoint>int</nonTechExpertPoint>
<nickName>string</nickName>
<username>string</username>
</profile>
<error>
<errId>int</errId>
<errInfo>string</errInfo>
<description>string</description>
</error>
</GetUserProfileResponse>
</soap12:Body>
</soap12:Envelope>
其中有用的就是 <GetUserProfileResult>boolean</GetUserProfileResult>
由於返回的是xml,可以採用
1 jdom等xml解析工具,我這個例子使用了jdom進行解析
2 可以直接用正則表達式
我們看代碼
// 使用jdom進行解析
SAXBuilder builder = new SAXBuilder();
Document doc = null;
Reader in = new StringReader(textXml);
try {
doc = builder.build(in);
Element root = doc.getRootElement();
List ls = root.getChildren();// 注意此處取出的是root節點下面的一層的Element集合
Element body = (Element) ls.get(0);
Element response = (Element) body.getChildren().get(0);
List content = response.getChildren();
Element result = (Element) content.get(0);
// 這個是運行結果的數據
if ("GetUserProfileResult".equals(result.getName())) {
// 失敗
if ("true".equals(result.getText())) {
return true;
}
} else {
return false;
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (JDOMException ex) {
ex.printStackTrace();
}
總結:
我們可以通過系統提供的一個並非檢測登錄的服務接口,實現對用戶登錄信息的檢測。我的網站可以使用csdn的用戶名和密碼登錄,就是調用了這個方法進行判斷。
完整的代碼
/**
* 檢查用戶名和密碼。
*
* @author 趙學慶,www.java2000.net
* @param username 用戶名
* @param password 密碼
* @return 如果成功,則返回true,失敗返回false
*/
public static boolean checkLogin(String username, String password) {
// 以下構造提交用的web服務的內容
StringBuilder b = new StringBuilder();
b.append("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
b.append("<soap12:Envelope xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" ");
b.append("xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" ");
b.append("xmlns:soap12=/"http://www.w3.org/2003/05/soap-envelope/">");
b.append("<soap12:Body>");
// 這裏是調用的功能標識
b.append(" <GetUserProfile xmlns=/"http://www.csdn.net//">");
// 這裏是調用的用戶信息標識
b.append(" <identity>");
// 登錄用戶名
b.append(" <username>" + username + "</username>");
// 登錄密碼
b.append(" <password>" + password + "</password>");
b.append(" </identity>");
// 要查看的用戶名,和登錄用戶相同
b.append(" <username>" + username + "</username>");
b.append(" </GetUserProfile>");
b.append("</soap12:Body>");
b.append("</soap12:Envelope>");
// 提交數據,並獲取返回結果
String textXml = postPage("http://forum.csdn.net/OpenApi/forumapi.asmx", "forum.csdn.net",
null, b.toString())[1];
// 使用jdom進行解析
SAXBuilder builder = new SAXBuilder();
Document doc = null;
Reader in = new StringReader(textXml);
try {
doc = builder.build(in);
Element root = doc.getRootElement();
List ls = root.getChildren();// 注意此處取出的是root節點下面的一層的Element集合
Element body = (Element) ls.get(0);
Element response = (Element) body.getChildren().get(0);
List content = response.getChildren();
Element result = (Element) content.get(0);
// 這個是運行結果的數據
if ("GetUserProfileResult".equals(result.getName())) {
// 失敗
if ("true".equals(result.getText())) {
return true;
}
} else {
return false;
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (JDOMException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 提交數據到指定的地址
*
* @param urlTo 指定提交的地址
* @param host 主機名稱
* @param cookies cookies的數據
* @param data 提交的數據
* @return 返回提交的結果數組,[0] 爲返回的cookie 數據,[1]爲返回的內容本體數據
*/
private static String[] postPage(String urlTo, String host, String cookies, String data) {
try {
URL url = new URL(urlTo);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true); // POST方式
con.setRequestMethod("POST");
con.addRequestProperty("Host", host);
con.addRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
if (cookies != null) {
con.addRequestProperty("Cookie", cookies);
}
byte[] bs = data.getBytes("UTF-8");
con.addRequestProperty("Content-Length", Integer.toString(bs.length));
OutputStream os = con.getOutputStream(); // 輸出流,寫數據
os.write(bs);
Map<String, List<String>> map = con.getHeaderFields();
String[] rtn = new String[2];
String cookie = "";
if (map.get("Set-Cookie") != null)
for (String v : map.get("Set-Cookie")) {
cookie += v.substring(0, v.indexOf(";") + 1);
}
rtn[0] = cookie;
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(),
"UTF-8")); // 讀取結果
String line;
StringBuilder b = new StringBuilder();
while ((line = reader.readLine()) != null) {
b.append(line);
}
reader.close();
rtn[1] = b.toString();
return rtn;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}