예제 #1
1
 private Map<String, String> parseHeaders(HttpURLConnection conn) {
   Map<String, String> headers = new HashMap<String, String>();
   for (String key : conn.getHeaderFields().keySet()) {
     headers.put(key, conn.getHeaderFields().get(key).get(0));
   }
   return headers;
 }
예제 #2
0
  /**
   * 登陆百度,其他方法调用之前需要先登陆
   *
   * @param username
   * @param password
   */
  public void login(String username, String password) {
    try {
      URL url = new URL("http://www.baidu.com/");
      HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
      httpUrlConnection.setRequestMethod("GET");
      String cookie1 = httpUrlConnection.getHeaderField("Set-Cookie");
      // System.out.println("cookie1:"+cookie1);
      cookie1 = cookie1.substring(0, 45);

      url = new URL("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true");
      httpUrlConnection = (HttpURLConnection) url.openConnection();
      httpUrlConnection.setRequestMethod("GET");
      httpUrlConnection.setRequestProperty("Cookie", cookie1);
      // httpUrlConnection.connect();
      String cookie2 = httpUrlConnection.getHeaderField("Set-Cookie");
      System.out.println("cookie2:" + cookie2);
      cookie2 = cookie2.substring(0, 11);
      String response = getResponse(httpUrlConnection.getInputStream());
      // System.out.println(response);
      Pattern pattern = Pattern.compile("token='(\\w+)'");
      Matcher matcher = pattern.matcher(response);
      matcher.find();
      String token = matcher.group(1);

      url = new URL("https://passport.baidu.com/v2/api/?login");
      httpUrlConnection = (HttpURLConnection) url.openConnection();
      httpUrlConnection.setRequestMethod("POST");
      // System.out.println(cookie1+"; "+cookie2);
      httpUrlConnection.setRequestProperty("Cookie", cookie1 + "; " + cookie2);
      httpUrlConnection.setDoOutput(true);
      // System.out.println(token);

      String querystring =
          "loginType=1&tpl=mn&token="
              + token
              + "&username="******"&password="******"&mem_pass=on";
      httpUrlConnection.getOutputStream().write(querystring.getBytes());
      httpUrlConnection.getOutputStream().flush();
      httpUrlConnection.getOutputStream().close();

      response = getResponse(httpUrlConnection.getInputStream());
      // System.out.println(response);

      // String cookie3=httpUrlConnection.getHeaderField("Set-Cookie");
      // System.out.println("cookie3:"+cookie3);

      // 获取登陆后的cookie
      Map<String, List<String>> hfs = httpUrlConnection.getHeaderFields();
      List<String> loginCookies = hfs.get("Set-Cookie");
      for (String cookie : loginCookies) {
        cookieValue += cookie.substring(0, cookie.indexOf(";") + 1);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
예제 #3
0
    @Override
    public InputSource resolveEntity(String publicId, String systemId)
        throws SAXException, IOException {
      InputSource inputSource = null;

      if (options.entityResolver != null) {
        inputSource = options.entityResolver.resolveEntity(null, systemId);
      }
      if (inputSource == null) {
        inputSource = new InputSource(systemId);
        InputStream is = null;
        int redirects = 0;
        boolean redirect;
        URL url = JAXWSUtils.getFileOrURL(inputSource.getSystemId());
        URLConnection conn = url.openConnection();
        do {
          if (conn instanceof HttpsURLConnection) {
            if (options.disableSSLHostnameVerification) {
              ((HttpsURLConnection) conn).setHostnameVerifier(new HttpClientVerifier());
            }
          }
          redirect = false;
          if (conn instanceof HttpURLConnection) {
            ((HttpURLConnection) conn).setInstanceFollowRedirects(false);
          }

          if (conn instanceof JarURLConnection) {
            if (conn.getUseCaches()) {
              doReset = true;
              conn.setDefaultUseCaches(false);
              c = conn;
            }
          }

          try {
            is = conn.getInputStream();
            // is = sun.net.www.protocol.http.HttpURLConnection.openConnectionCheckRedirects(conn);
          } catch (IOException e) {
            if (conn instanceof HttpURLConnection) {
              HttpURLConnection httpConn = ((HttpURLConnection) conn);
              int code = httpConn.getResponseCode();
              if (code == 401) {
                errorReceiver.error(
                    new SAXParseException(
                        WscompileMessages.WSIMPORT_AUTH_INFO_NEEDED(
                            e.getMessage(), systemId, WsimportOptions.defaultAuthfile),
                        null,
                        e));
                throw new AbortException();
              }
              // FOR other code we will retry with MEX
            }
            throw e;
          }

          // handle 302 or 303, JDK does not seem to handle 302 very well.
          // Need to redesign this a bit as we need to throw better error message for IOException in
          // this case
          if (conn instanceof HttpURLConnection) {
            HttpURLConnection httpConn = ((HttpURLConnection) conn);
            int code = httpConn.getResponseCode();
            if (code == 302 || code == 303) {
              // retry with the value in Location header
              List<String> seeOther = httpConn.getHeaderFields().get("Location");
              if (seeOther != null && seeOther.size() > 0) {
                URL newurl = new URL(url, seeOther.get(0));
                if (!newurl.equals(url)) {
                  errorReceiver.info(
                      new SAXParseException(
                          WscompileMessages.WSIMPORT_HTTP_REDIRECT(code, seeOther.get(0)), null));
                  url = newurl;
                  httpConn.disconnect();
                  if (redirects >= 5) {
                    errorReceiver.error(
                        new SAXParseException(
                            WscompileMessages.WSIMPORT_MAX_REDIRECT_ATTEMPT(), null));
                    throw new AbortException();
                  }
                  conn = url.openConnection();
                  inputSource.setSystemId(url.toExternalForm());
                  redirects++;
                  redirect = true;
                }
              }
            }
          }
        } while (redirect);
        inputSource.setByteStream(is);
      }

      return inputSource;
    }