CSDN--OpenAPI的使用代码-判断用户名和密码

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;
    }
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值