示例#1
0
  public static void fetchNextPage(Integer pageNo, HttpClient client) throws Exception {
    NameValuePair[] data = {
      new NameValuePair(
          "User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:25.0) Gecko/20100101 Firefox/25.0"),
      new NameValuePair("__EVENTARGUMENT", ""),
      new NameValuePair("__EVENTTARGET", ""),
      new NameValuePair("__VIEWSTATE", viewState),
      new NameValuePair("__EVENTVALIDATION", eventValidation),
      new NameValuePair("ctl00%24ContentPlaceHolder1%24ImplementDayDDList", "0"),
      new NameValuePair("ctl00%24ContentPlaceHolder1%24ImplementMonthDDList", "0"),
      new NameValuePair("ctl00%24ContentPlaceHolder1%24PropertyDDL", "2"),
      new NameValuePair("ctl00%24ContentPlaceHolder1%24PublishedDayDDList", "0"),
      new NameValuePair("ctl00%24ContentPlaceHolder1%24PublishedMonthDDList", "0"),
      new NameValuePair("ctl00$ContentPlaceHolder1$btnNext.x", btn_x),
      new NameValuePair("ctl00$ContentPlaceHolder1$btnNext.y", btn_y),
      new NameValuePair("ctl00%24ContentPlaceHolder1%24pageNo", ""),
      new NameValuePair("ctl00%24ContentPlaceHolder1%24statusDDList", "2"),
      new NameValuePair(
          "ctl00_ContentPlaceHolder1_categoryTreeView_ExpandState",
          "enennnnnnnnnnnnnnnnnnnnnnnnnnnn")
    };

    PostMethod post = new PostMethod("http://www.catarc.org.cn/standard/SearchStandard.aspx");
    post.setRequestBody(data);
    client.executeMethod(post);

    String html = post.getResponseBodyAsString();

    setViewState(html);

    setEventValidation(html);

    setStandardIdsToMap(pageNo, html);
  }
示例#2
0
  /**
   * 执行一个HTTP POST 请求,返回请求响应的HTML
   *
   * @param url 请求的URL 地址
   * @param params 请求的查询参数,可以为null
   * @return 返回请求响应的HTML
   */
  private static String doPost(String url, Map<String, String> params) {
    String response = null;
    HttpClient client = new HttpClient();
    PostMethod postMethod = new PostMethod(url);
    postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
    // 设置Post 数据
    if (!params.isEmpty()) {
      int i = 0;
      NameValuePair[] data = new NameValuePair[params.size()];
      for (Entry<String, String> entry : params.entrySet()) {
        data[i] = new NameValuePair(entry.getKey(), entry.getValue());
        i++;
      }
      postMethod.setRequestBody(data);
    }
    try {

      client.executeMethod(postMethod);
      if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
        response = postMethod.getResponseBodyAsString();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      postMethod.releaseConnection();
    }
    return response;
  }
  public String getContextByPostMethod(String url, List<KeyValue> params) {
    HttpClient client = getHttpClient();

    client.getParams().setParameter("http.protocol.content-charset", this.codeing);

    PostMethod post = null;
    String result = "";
    try {
      URL u = new URL(url);
      client
          .getHostConfiguration()
          .setHost(
              u.getHost(), u.getPort() == -1 ? u.getDefaultPort() : u.getPort(), u.getProtocol());

      post = new PostMethod(u.getPath());

      NameValuePair[] nvps = new NameValuePair[params.size()];
      int i = 0;
      for (KeyValue kv : params) {
        nvps[i] = new NameValuePair(kv.getKey(), kv.getValue());
        i++;
      }

      post.setRequestBody(nvps);

      client.executeMethod(post);
      result = post.getResponseBodyAsString();
    } catch (Exception e) {
      throw new NetException("HttpClient catch!", e);
    } finally {
      if (post != null) post.releaseConnection();
    }
    return result;
  }
示例#4
0
  /**
   * 鍙栧緱ST
   *
   * @param server
   * @param ticketGrantingTicket
   * @param service
   */
  private static String getServiceTicket(
      final String server, final String ticketGrantingTicket, final String service) {
    if (ticketGrantingTicket == null) return null;

    final HttpClient client = new HttpClient();

    final PostMethod post = new PostMethod(server + "/" + ticketGrantingTicket);

    post.setRequestBody(new NameValuePair[] {new NameValuePair("service", service)});

    try {
      client.executeMethod(post);

      final String response = post.getResponseBodyAsString();

      switch (post.getStatusCode()) {
        case 200:
          return response;

        default:
          warning("Invalid response code (" + post.getStatusCode() + ") from CAS server!");
          info("Response (1k): " + response.substring(0, Math.min(1024, response.length())));
          break;
      }
    } catch (final IOException e) {
      warning(e.getMessage());
    } finally {
      post.releaseConnection();
    }

    return null;
  }
  private String getEditorResponse(String urlpath, String encProcessSrc) {
    HttpClient httpclient = new HttpClient();

    PostMethod authMethod = new PostMethod(urlpath);
    NameValuePair[] data = {
      new NameValuePair("j_username", "admin"), new NameValuePair("j_password", "admin")
    };
    authMethod.setRequestBody(data);
    try {
      httpclient.executeMethod(authMethod);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    } finally {
      authMethod.releaseConnection();
    }

    PostMethod theMethod = new PostMethod(urlpath);
    theMethod.setParameter("processsource", encProcessSrc);
    StringBuffer sb = new StringBuffer();
    try {
      httpclient.executeMethod(theMethod);
      sb.append(theMethod.getResponseBodyAsString());
      return sb.toString();

    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } finally {
      theMethod.releaseConnection();
    }
  }
示例#6
0
  public static void send(String content, String mobile) throws Exception {

    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(Url);

    client.getParams().setContentCharset("UTF-8");
    method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

    NameValuePair[] data = { // 提交短信
      new NameValuePair("account", "cf_ddz"),
      new NameValuePair("password", "zaq12wsx"), // 密码可以使用明文密码或使用32位MD5加密
      new NameValuePair("mobile", mobile),
      new NameValuePair("content", content),
    };

    method.setRequestBody(data);

    client.executeMethod(method);

    String SubmitResult = method.getResponseBodyAsString();

    Document doc = DocumentHelper.parseText(SubmitResult);
    Element root = doc.getRootElement();

    String code = root.elementText("code");
    String msg = root.elementText("msg");
    String smsid = root.elementText("smsid");

    logger.info(code + "|" + msg + "|" + smsid);
  }
 public String getHtmlByParamAndCode(NameValuePair[] param, String code) {
   StringBuffer contentStr = new StringBuffer();
   PostMethod postMethod = null;
   init();
   try {
     postMethod = new PostMethod(this.getUrl());
     postMethod.setRequestBody(param);
     postMethod.setRequestHeader(
         "User-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); // ä¯ÀÀÆ÷αÔì
     client.executeMethod(postMethod);
     InputStream in = postMethod.getResponseBodyAsStream();
     BufferedReader br = new BufferedReader(new InputStreamReader(in, code), 1024);
     String line = "";
     while ((line = br.readLine()) != null) {
       contentStr.append(line);
     }
     if (br != null) {
       br.close();
       br = null;
     }
     if (in != null) {
       in.close();
       in = null;
     }
   } catch (Exception e) {
     e.printStackTrace();
     release();
   } finally {
     postMethod.releaseConnection();
   }
   return contentStr.toString();
 }
  // http post
  public static String httpPost(String url, String body) throws Exception {
    ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
    Protocol.registerProtocol("https", new Protocol("https", fcty, 443));

    PostMethod post = new PostMethod(url);

    post.setRequestBody(body); // 这里添加xml字符串

    // 指定请求内容的类型
    post.setRequestHeader("Content-type", "application/json; charset=utf-8");
    HttpClient httpclient = new HttpClient(); // 创建 HttpClient 的实例

    httpclient.executeMethod(post);
    String res = post.getResponseBodyAsString();

    logger.info(res);

    post.releaseConnection(); // 释放连接

    if (res.indexOf("40001") > -1) {
      getFreshAccessToken();
    }

    return res;
  }
示例#9
0
  public static String postXml(String url, String xml) {
    String txt = null;
    PostMethod post = new PostMethod(url); // 请求地址
    post.setRequestBody(xml); // 这里添加xml字符串

    // 指定请求内容的类型
    post.setRequestHeader("Content-type", "text/xml; charset=iso-8859-1");
    HttpClient httpclient = new HttpClient(); // 创建 HttpClient 的实例
    int result;
    try {
      result = httpclient.executeMethod(post);
      System.out.println("Response status code: " + result); // 返回200为成功
      System.out.println("Response body: ");

      BufferedReader reader =
          new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream(), "ISO-8859-1"));
      String tmp = null;
      String htmlRet = "";
      while ((tmp = reader.readLine()) != null) {
        htmlRet += tmp + "\r\n";
      }
      txt = new String(htmlRet.getBytes("ISO-8859-1"), "UTF-8");
      System.out.println(txt);

      // txt = post.getResponseBodyAsString();
      // System.out.println(post.getResponseBodyAsString());// 返回的内容
      post.releaseConnection(); // 释放连接
    } catch (HttpException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return txt;
  }
示例#10
0
  public static String postRequest(String url, Map<String, String> parameters) {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(url);
    method
        .getParams()
        .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
    if (MapUtils.isNotEmpty(parameters)) {
      NameValuePair[] pairs = new NameValuePair[parameters.size()];
      Iterator<Entry<String, String>> iterator = parameters.entrySet().iterator();
      int index = 0;
      while (iterator.hasNext()) {
        Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
        pairs[index++] = new NameValuePair(entry.getKey(), entry.getValue());
      }
      method.setRequestBody(pairs);
    }

    try {
      int statusCode = client.executeMethod(method);
      if (statusCode != HttpStatus.SC_OK) {
        logger.error("post method fail.url={},status={}", url, statusCode);
        return "";
      }
      byte[] responseBody = method.getResponseBody();
      return new String(responseBody, "UTF-8");
    } catch (Exception e) {
      logger.error("url=" + url, e);
      return "";
    } finally {
      method.releaseConnection();
    }
  }
示例#11
0
 protected void doAuthorization(
     HttpClient httpclient, HttpMethod method, Map<String, Object> params) {
   if (type == null) {
     return;
   }
   String u = (String) params.get("Username");
   String p = (String) params.get("Password");
   if (u == null || p == null) {
     u = this.username;
     p = this.password;
   }
   if (u == null) {
     throw new IllegalArgumentException("Could not find username");
   }
   if (p == null) {
     throw new IllegalArgumentException("Could not find password");
   }
   if (type == AuthenticationType.BASIC) {
     httpclient
         .getState()
         .setCredentials(
             new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
             new UsernamePasswordCredentials(u, p));
     method.setDoAuthentication(true);
   } else if (type == AuthenticationType.FORM_BASED) {
     String authUrlStr = (String) params.get("AuthUrl");
     if (authUrlStr == null) {
       authUrlStr = authUrl;
     }
     if (authUrlStr == null) {
       throw new IllegalArgumentException("Could not find authentication url");
     }
     try {
       httpclient.executeMethod(method);
     } catch (IOException e) {
       throw new RuntimeException("Could not execute request for form-based authentication", e);
     } finally {
       method.releaseConnection();
     }
     PostMethod authMethod = new PostMethod(authUrlStr);
     NameValuePair[] data = {
       new NameValuePair("j_username", u), new NameValuePair("j_password", p)
     };
     authMethod.setRequestBody(data);
     try {
       httpclient.executeMethod(authMethod);
     } catch (IOException e) {
       throw new RuntimeException("Could not initialize form-based authentication", e);
     } finally {
       authMethod.releaseConnection();
     }
   } else {
     throw new RuntimeException("Unknown AuthenticationType " + type);
   }
 }
示例#12
0
 @Test
 public void findCoverProject() throws Exception {
   HttpClient httpClient = new HttpClient();
   // String url = "http://localhost:8080/rental/rent/entry.html";
   String url = "http://localhost:8080/rental/pay/unify";
   PostMethod postMethod = new PostMethod(url);
   String charSet = "UTF-8";
   httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charSet);
   String a =
       "<xml><a>1</a><return_code>SUCCESS</return_code><return_msg></return_msg><appid>wx8276ac869116d5aa</appid><mch_id>10058488</mch_id><nonce_str></nonce_str><sign></sign><result_code>SUCCESS</result_code><err_code></err_code><err_code_des></err_code_des><openid>o00MJjxDIObA_dpqLSnnewODPVx8</openid><is_subscribe>N</is_subscribe><trade_type>JSAPI</trade_type><bank_type>bankType</bank_type><total_fee>1000</total_fee><coupon_fee></coupon_fee><fee_type></fee_type><transaction_id>12345678901234567890123456789012</transaction_id><out_trade_no>test7764262942</out_trade_no><time_end>20141231100001</time_end></xml>";
   postMethod.setRequestBody(a);
   int statusCode = httpClient.executeMethod(postMethod);
   System.out.println(postMethod.getResponseBodyAsString());
 }
示例#13
0
 public static String HttpPost(String url, String method, Map parmMap) throws Exception {
   String encoding = "UTF-8";
   String webUrl = url + "/" + method;
   if (encoding == null || "".equals(encoding)) encoding = "UTF-8";
   StringBuffer sBuffer = new StringBuffer();
   // 构造HttpClient的实例
   HttpClient httpClient = new HttpClient();
   // 创建POS方法的实例
   NameValuePair[] pairs = null;
   PostMethod postMethod = new PostMethod(webUrl);
   if (parmMap != null) {
     pairs = new NameValuePair[parmMap.size()];
     Set set = parmMap.keySet();
     Iterator it = set.iterator();
     int i = 0;
     while (it.hasNext()) {
       Object key = it.next();
       Object value = parmMap.get(key);
       pairs[i] = new NameValuePair(key.toString(), value.toString());
       i++;
     }
     postMethod.setRequestBody(pairs);
   }
   postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding);
   try {
     // 执行getMethod
     int statusCode = httpClient.executeMethod(postMethod);
     if (statusCode != HttpStatus.SC_OK) {
       System.err.println("Method failed: " + postMethod.getStatusLine());
     }
     InputStream resStream = postMethod.getResponseBodyAsStream();
     sBuffer = new StringBuffer(postMethod.getResponseBodyAsString() + "");
     // modify by guanshiqiang at 2012-06-01 for 处理接收字符乱码bug end
     // 处理内容
     // sBuffer.append(new String(responseBody,encoding));
   } catch (HttpException e) {
     // 发生致命的异常,可能是协议不对或者返回的内容有问题
     System.out.println("Please check your provided http address!");
     e.printStackTrace();
     throw e;
   } catch (IOException e) {
     e.printStackTrace();
     throw e;
   } finally {
     // 释放连接
     postMethod.releaseConnection();
   }
   return sBuffer.toString();
 }
 public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile)
     throws IOException {
   HttpClient client = new HttpClient();
   NameValuePair[] nameValuePairs = new NameValuePair[4];
   nameValuePairs[0] = new NameValuePair("apikey", apikey);
   nameValuePairs[1] = new NameValuePair("tpl_id", String.valueOf(tpl_id));
   nameValuePairs[2] = new NameValuePair("tpl_value", tpl_value);
   nameValuePairs[3] = new NameValuePair("mobile", mobile);
   PostMethod method = new PostMethod(URI_TPL_SEND_SMS);
   method.setRequestBody(nameValuePairs);
   HttpMethodParams param = method.getParams();
   param.setContentCharset(ENCODING);
   client.executeMethod(method);
   return method.getResponseBodyAsString();
 }
示例#15
0
  public static String HttpPostForLogistics(String url, String method, String str) {
    String encoding = "UTF-8";
    String webUrl = url + "/" + method;
    StringBuffer sBuffer = new StringBuffer();
    // 构造HttpClient的实例
    HttpClient httpClient = new HttpClient();
    // 创建POS方法的实例
    PostMethod postMethod = new PostMethod(webUrl);
    try {
      // postMethod.setRequestEntity(new ByteArrayRequestEntity(str.getBytes(),"application/json;
      // charset=utf-8"));
      postMethod.setRequestBody(str);
    } catch (Exception e1) {
      e1.printStackTrace();
    }
    postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding);
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000); // 连接5秒超时
    httpClient.getHttpConnectionManager().getParams().setSoTimeout(30000); // 读取30秒超时
    // postMethod.setRequestHeader("Content-type", "application/json; charset=utf-8");
    // postMethod.setDoAuthentication(false);
    // postMethod.addRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT
    // 5.1)");

    postMethod.setDoAuthentication(true);
    postMethod.addRequestHeader("Authorization", "Basic U0FQX0FNOkFubmllTWFvMTIzNA==");
    postMethod.setRequestHeader("Content-type", "application/json");
    try {
      // 执行getMethod
      int statusCode = httpClient.executeMethod(postMethod);
      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + postMethod.getStatusLine());
        sBuffer = new StringBuffer();
      } else {
        sBuffer = new StringBuffer(postMethod.getResponseBodyAsString() + "");
      }
    } catch (HttpException e) {
      // 发生致命的异常,可能是协议不对或者返回的内容有问题
      System.out.println("Please check your provided http address!");
      e.printStackTrace();
    } catch (IOException e) {
      // 发生网络异常
      e.printStackTrace();
    } finally {
      // 释放连接
      postMethod.releaseConnection();
    }
    return sBuffer.toString();
  }
 /** Test that the body can be set as a array of parameters */
 public void testParametersBodyToParamServlet() throws Exception {
   PostMethod method = new PostMethod("/");
   NameValuePair[] parametersBody =
       new NameValuePair[] {
         new NameValuePair("pname1", "pvalue1"), new NameValuePair("pname2", "pvalue2")
       };
   method.setRequestBody(parametersBody);
   this.server.setHttpService(new EchoService());
   try {
     this.client.executeMethod(method);
     assertEquals(200, method.getStatusCode());
     String body = method.getResponseBodyAsString();
     assertEquals("pname1=pvalue1&pname2=pvalue2", body);
   } finally {
     method.releaseConnection();
   }
 }
示例#17
0
 /**
  * Test that {@link PostMethod#addParameter(java.lang.String,java.lang.String)} and {@link
  * PostMethod#setQueryString(java.lang.String)} combine properly.
  */
 public void testPostMethodParameterAndQueryString() throws Exception {
   this.server.setHttpService(new QueryInfoService());
   PostMethod method = new PostMethod("/");
   method.setQueryString("query=string");
   method.setRequestBody(
       new NameValuePair[] {
         new NameValuePair("param", "eter"), new NameValuePair("para", "meter")
       });
   try {
     this.client.executeMethod(method);
     assertEquals(200, method.getStatusCode());
     String response = method.getResponseBodyAsString();
     assertTrue(response.indexOf("QueryString=\"query=string\"") >= 0);
     assertFalse(response.indexOf("QueryString=\"param=eter&para=meter\"") >= 0);
   } finally {
     method.releaseConnection();
   }
 }
示例#18
0
 private HttpMethod createPostMethod(String url, HttpData httpData) {
   PostMethod method = new InnerPostMethod(url);
   if (httpData != null) {
     List<NameValuePair> datalist = new ArrayList<NameValuePair>(httpData.getPatameterCount());
     Set<Map.Entry<String, String>> set = httpData.getParameterMap().entrySet();
     for (Map.Entry<String, String> e : set) {
       datalist.add(new NameValuePair(e.getKey(), e.getValue()));
     }
     Set<Entry<String, List<String>>> bset = httpData.getBatchParameterMap().entrySet();
     for (Entry<String, List<String>> e : bset) {
       for (String s : e.getValue()) {
         datalist.add(new NameValuePair(e.getKey(), s));
       }
     }
     method.setRequestBody(datalist.toArray(new NameValuePair[datalist.size()]));
   }
   return method;
 }
  public boolean populate(CrawlURI curi, HttpClient http, HttpMethod method, String payload) {
    // http is not used.
    // payload is not used.
    boolean result = false;
    Map formItems = null;
    try {
      formItems = getFormItems(curi);
    } catch (AttributeNotFoundException e1) {
      logger.severe("Failed get of form items for " + curi);
    }
    if (formItems == null || formItems.size() <= 0) {
      try {
        logger.severe("No form items for " + method.getURI());
      } catch (URIException e) {
        logger.severe("No form items and exception getting uri: " + e.getMessage());
      }
      return result;
    }

    NameValuePair[] data = new NameValuePair[formItems.size()];
    int index = 0;
    String key = null;
    for (Iterator i = formItems.keySet().iterator(); i.hasNext(); ) {
      key = (String) i.next();
      data[index++] = new NameValuePair(key, (String) formItems.get(key));
    }
    if (method instanceof PostMethod) {
      ((PostMethod) method).setRequestBody(data);
      result = true;
    } else if (method instanceof GetMethod) {
      // Append these values to the query string.
      // Get current query string, then add data, then get it again
      // only this time its our data only... then append.
      HttpMethodBase hmb = (HttpMethodBase) method;
      String currentQuery = hmb.getQueryString();
      hmb.setQueryString(data);
      String newQuery = hmb.getQueryString();
      hmb.setQueryString(((currentQuery != null) ? currentQuery : "") + "&" + newQuery);
      result = true;
    } else {
      logger.severe("Unknown method type: " + method);
    }
    return result;
  }
示例#20
0
  public WebResponse doPost(String url, NameValuePair[] params, String referer)
      throws WebBrowserException, WebServerException {
    try {
      PostMethod postMethod = new PostMethod(url);
      setHttpRequestHeader(postMethod);
      if (referer != null && referer.trim().length() != 0) {
        postMethod.setRequestHeader("Referer", referer);
      }
      postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      postMethod.setRequestBody(params);
      logHttpPostRequest(postMethod);
      int status = httpClient.executeMethod(postMethod);
      String strResp = postMethod.getResponseBodyAsString();
      byte[] byteResp = postMethod.getResponseBody();
      String respEnc = getResponseEncoding(postMethod);
      logHttpResponse(postMethod, strResp);
      postMethod.releaseConnection();

      // http:301,302,303,307
      if (status == HttpStatus.SC_MOVED_PERMANENTLY
          || status == HttpStatus.SC_MOVED_TEMPORARILY
          || status == HttpStatus.SC_SEE_OTHER
          || status == HttpStatus.SC_TEMPORARY_REDIRECT) {
        Header locationHeader = postMethod.getResponseHeader("Location");
        String location = locationHeader.getValue();
        if (logger.isDebugEnabled()) {
          logger.debug("Redirect To Location = " + location);
        }
        if (location.startsWith("http")) {
          return doGet(location);
        } else {
          return doGet("http://" + getResponseHost(postMethod) + location);
        }
      } else if (status == HttpStatus.SC_OK) { // http:200
        return new WebResponse(postMethod.getURI().toString(), byteResp, respEnc);
      } else {
        throw new WebServerException("Server Exception[code=" + status + "]");
      }
    } catch (HttpException e) {
      throw new WebBrowserException(e);
    } catch (IOException e) {
      throw new WebBrowserException(e);
    }
  }
示例#21
0
 public String getHtmlByParam(NameValuePair[] param) {
   String contentStr = "";
   PostMethod postMethod = null;
   init();
   try {
     postMethod = new PostMethod(this.getUrl());
     postMethod.setRequestBody(param);
     postMethod.setRequestHeader(
         "User-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); // ä¯ÀÀÆ÷αÔì
     client.executeMethod(postMethod);
     contentStr = postMethod.getResponseBodyAsString();
   } catch (Exception e) {
     e.printStackTrace();
     release();
   } finally {
     postMethod.releaseConnection();
   }
   return contentStr;
 }
示例#22
0
  /**
   * post请求方法
   *
   * @param url
   * @return
   * @throws IOException
   */
  public static PostMethod postMethod(String url, Map<String, String> parameter)
      throws IOException {
    PostMethod post = new PostMethod(url);
    post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    NameValuePair[] params = new NameValuePair[parameter.keySet().size()];
    Iterator<String> it = parameter.keySet().iterator();
    int i = 0;
    String key = "";
    while (it.hasNext()) {
      key = it.next();
      params[i] = new NameValuePair(key, parameter.get(key));
      i++;
    }
    //			{ new NameValuePair("eIdListString", parameter)};
    post.setRequestBody(params);
    post.releaseConnection();

    return post;
  }
示例#23
0
  private static String[] httpPost(String url, String xml) throws Exception {

    HttpClient client = new HttpClient();

    HttpState state = client.getState();

    Credentials credentials = new UsernamePasswordCredentials("S2VMS", "S2VMS!Q@W#E");
    state.setCredentials(null, null, credentials);
    client.setState(state);
    PostMethod method = new PostMethod(url);

    method.setDoAuthentication(true);

    method.getHostAuthState().setAuthAttempted(true);
    method.getHostAuthState().setAuthRequested(true);
    method.getHostAuthState().setPreemptive();
    method.addRequestHeader("Content-Type", "text/xml");
    method.addRequestHeader("SOAPAction", "http://tempuri.org/GetLinkMedia");

    try {
      method.setRequestBody(xml);

    } catch (Exception e) {
      try {
        ByteArrayRequestEntity entity = new ByteArrayRequestEntity(xml.getBytes());
        method.setRequestEntity(entity);

      } catch (Exception e1) {
        throw new Exception("Impossible to set the xml in the post");
      }
    }

    int iRes = client.executeMethod(method);

    byte[] response = method.getResponseBody();

    String textResponse = new String(response);

    String[] toReturn = {"" + iRes, textResponse};

    return toReturn;
  }
  protected void tearDown() {

    // Delete the content
    PostMethod post_delete = new PostMethod(SLING_URL + COLLECTION_URL + slug);
    NameValuePair[] data_delete = {
      new NameValuePair(":operation", "delete"),
    };

    post_delete.setDoAuthentication(true);
    post_delete.setRequestBody(data_delete);

    try {
      client.executeMethod(post_delete);
    } catch (HttpException e1) {
      e1.printStackTrace();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    post_delete.releaseConnection();
  }
示例#25
0
  /**
   * @param server
   * @param username
   * @param password
   */
  private static String getTicketGrantingTicket(
      final String server, final String username, final String password) {
    final HttpClient client = new HttpClient();

    final PostMethod post = new PostMethod(server);

    post.setRequestBody(
        new NameValuePair[] {
          new NameValuePair("username", username), new NameValuePair("password", password)
        });

    try {
      client.executeMethod(post);

      final String response = post.getResponseBodyAsString();
      info("TGT=" + response);
      switch (post.getStatusCode()) {
        case 201:
          {
            final Matcher matcher = Pattern.compile(".*action=\".*/(.*?)\".*").matcher(response);

            if (matcher.matches()) return matcher.group(1);

            warning("Successful ticket granting request, but no ticket found!");
            info("Response (1k): " + response.substring(0, Math.min(1024, response.length())));
            break;
          }

        default:
          warning("Invalid response code (" + post.getStatusCode() + ") from CAS server!");
          info("Response (1k): " + response.substring(0, Math.min(1024, response.length())));
          break;
      }
    } catch (final IOException e) {
      warning(e.getMessage());
    } finally {
      post.releaseConnection();
    }

    return null;
  }
示例#26
0
 /**
  * Sets up the given {@link PostMethod} to send the same standard POST data as was sent in the
  * given {@link HttpServletRequest}
  *
  * @param postMethodProxyRequest The {@link PostMethod} that we are configuring to send a standard
  *     POST request
  * @param httpServletRequest The {@link HttpServletRequest} that contains the POST data to be sent
  *     via the {@link PostMethod}
  */
 @SuppressWarnings("unchecked")
 private void handleStandardPost(
     PostMethod postMethodProxyRequest, HttpServletRequest httpServletRequest) {
   // Get the client POST data as a Map
   Map<String, String[]> mapPostParameters =
       (Map<String, String[]>) httpServletRequest.getParameterMap();
   // Create a List to hold the NameValuePairs to be passed to the PostMethod
   List<NameValuePair> listNameValuePairs = new ArrayList<NameValuePair>();
   // Iterate the parameter names
   for (String stringParameterName : mapPostParameters.keySet()) {
     // Iterate the values for each parameter name
     String[] stringArrayParameterValues = mapPostParameters.get(stringParameterName);
     for (String stringParamterValue : stringArrayParameterValues) {
       // Create a NameValuePair and store in list
       NameValuePair nameValuePair = new NameValuePair(stringParameterName, stringParamterValue);
       listNameValuePairs.add(nameValuePair);
     }
   }
   // Set the proxy request POST data
   postMethodProxyRequest.setRequestBody(listNameValuePairs.toArray(new NameValuePair[] {}));
 }
示例#27
0
  /**
   * 登录
   *
   * @return
   * @throws IOException
   * @throws HttpException
   */
  public static HttpClient login() throws IOException, HttpException {
    HttpClient client = new HttpClient();
    client.getHostConfiguration().setHost("www.catarc.org.cn");
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    NameValuePair[] data = {
      new NameValuePair(
          "User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:25.0) Gecko/20100101 Firefox/25.0"),
      new NameValuePair("__VIEWSTATE", "/wEPDwUJNzE2NjczNjA4ZGQCaV1Dt0DOUg5r7O3ZD+sGL1Hq6Q=="),
      new NameValuePair(
          "__EVENTVALIDATION",
          "/wEWBQK/2uyqBgKl1bKzCQK1qbSRCwLZpaCZAwKTuvSuCTYE9T8nbPNCPEY2eqmCy5I3BwtG"),
      new NameValuePair("txtUserName", "asri"),
      new NameValuePair("txtPassword", "asri"),
      new NameValuePair("loginBtn", "%B5%C7%C2%BD")
    };
    PostMethod post = new PostMethod("http://www.catarc.org.cn/standard/Default.aspx");
    post.setRequestBody(data);
    client.executeMethod(post);

    return client;
  }
  protected boolean performAgeingCallback(
      UserDTO user, UserStatusDTO oldStatus, UserStatusDTO newStatus) {
    String url = null;
    try {
      PreferenceBL pref = new PreferenceBL();
      pref.set(user.getEntity().getId(), Constants.PREFERENCE_URL_CALLBACK);
      url = pref.getString();

    } catch (EmptyResultDataAccessException e) {
      /* ignore, no callback preference configured */
    }

    if (url != null && url.length() > 0) {
      try {
        LOG.debug("Performing ageing HTTP callback for URL: " + url);

        // cook the parameters to be sent
        NameValuePair[] data = new NameValuePair[6];
        data[0] = new NameValuePair("cmd", "ageing_update");
        data[1] = new NameValuePair("user_id", String.valueOf(user.getId()));
        data[2] = new NameValuePair("login_name", user.getUserName());
        data[3] = new NameValuePair("from_status", String.valueOf(oldStatus.getId()));
        data[4] = new NameValuePair("to_status", String.valueOf(newStatus.getId()));
        data[5] = new NameValuePair("can_login", String.valueOf(newStatus.getCanLogin()));

        // make the call
        HttpClient client = new HttpClient();
        client.setConnectionTimeout(30000);
        PostMethod post = new PostMethod(url);
        post.setRequestBody(data);
        client.executeMethod(post);

      } catch (Exception e) {
        LOG.error("Exception occurred posting ageing HTTP callback for URL: " + url, e);
        return false;
      }
    }
    return true;
  }
  protected void setUp() {

    Map<String, String> envs = System.getenv();
    Set<String> keys = envs.keySet();

    Iterator<String> it = keys.iterator();
    boolean hashost = false;
    while (it.hasNext()) {
      String key = (String) it.next();

      if (key.compareTo(HOSTVAR) == 0) {
        SLING_URL = SLING_URL + (String) envs.get(key);
        hashost = true;
      }
    }

    if (hashost == false) SLING_URL = SLING_URL + HOSTPREDEF;

    client = new HttpClient();
    title = "IT col";
    schema = "default";
    slug = "it_col";
    subtitle = "subtitle it";
    extern_storage = "on";

    authPrefs.add(AuthPolicy.DIGEST);
    authPrefs.add(AuthPolicy.BASIC);
    defaultcreds = new UsernamePasswordCredentials("admin", "admin");

    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(AuthScope.ANY, defaultcreds);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

    PostMethod post_create = new PostMethod(SLING_URL + COLLECTION_URL + slug);

    post_create.setDoAuthentication(true);

    NameValuePair[] data_create = {
      new NameValuePair("sling:resourceType", "gad/collection"),
      new NameValuePair("title", title),
      new NameValuePair("schema", schema),
      new NameValuePair("subtitle", subtitle),
      new NameValuePair("extern_storage", extern_storage),
      new NameValuePair("picture", ""),
      new NameValuePair("jcr:created", ""),
      new NameValuePair("jcr:createdBy", ""),
      new NameValuePair("jcr:lastModified", ""),
      new NameValuePair("jcr:lastModifiedBy", "")
    };
    post_create.setRequestBody(data_create);

    try {
      client.executeMethod(post_create);
    } catch (HttpException e1) {
      e1.printStackTrace();
    } catch (IOException e1) {
      e1.printStackTrace();
    }

    post_create.releaseConnection();
  }
示例#30
0
  private Object[] callHTTPPOST(Object[] rowData) throws KettleException {
    // get dynamic url ?
    if (meta.isUrlInField())
      data.realUrl = data.inputRowMeta.getString(rowData, data.indexOfUrlField);

    try {
      if (log.isDetailed())
        logDetailed(Messages.getString("HTTPPOST.Log.ConnectingToURL", data.realUrl));

      // Prepare HTTP POST
      //
      HttpClient HTTPPOSTclient = new HttpClient();
      PostMethod post = new PostMethod(data.realUrl);
      // post.setFollowRedirects(false);

      // Specify content type and encoding
      // If content encoding is not explicitly specified
      // ISO-8859-1 is assumed
      if (Const.isEmpty(data.realEncoding)) post.setRequestHeader("Content-type", "text/xml");
      else post.setRequestHeader("Content-type", "text/xml; " + data.realEncoding);

      // BODY PARAMETERS
      if (data.useBodyParameters) {
        // set body parameters that we want to send
        for (int i = 0; i < data.body_parameters_nrs.length; i++) {
          data.bodyParameters[i].setValue(
              data.inputRowMeta.getString(rowData, data.body_parameters_nrs[i]));
        }
        post.setRequestBody(data.bodyParameters);
      }

      // QUERY PARAMETERS
      if (data.useQueryParameters) {
        for (int i = 0; i < data.query_parameters_nrs.length; i++) {
          data.queryParameters[i].setValue(
              data.inputRowMeta.getString(rowData, data.query_parameters_nrs[i]));
        }
        post.setQueryString(data.queryParameters);
      }

      // Set request entity?
      if (data.indexOfRequestEntity >= 0) {
        String tmp = data.inputRowMeta.getString(rowData, data.indexOfRequestEntity);
        // Request content will be retrieved directly
        // from the input stream
        // Per default, the request content needs to be buffered
        // in order to determine its length.
        // Request body buffering can be avoided when
        // content length is explicitly specified

        if (meta.isPostAFile()) {
          File input = new File(tmp);
          post.setRequestEntity(
              new InputStreamRequestEntity(new FileInputStream(input), input.length()));
        } else {
          post.setRequestEntity(
              new InputStreamRequestEntity(new ByteArrayInputStream(tmp.getBytes()), tmp.length()));
        }
      }

      // Execute request
      //
      InputStream inputStream = null;
      try {
        // Execute the POST method
        int statusCode = HTTPPOSTclient.executeMethod(post);

        // Display status code
        if (log.isDebug())
          log.logDebug(
              toString(), Messages.getString("HTTPPOST.Log.ResponseCode", "" + statusCode));
        String body = null;
        if (statusCode != -1) {
          // the response
          inputStream = post.getResponseBodyAsStream();
          StringBuffer bodyBuffer = new StringBuffer();
          int c;
          while ((c = inputStream.read()) != -1) bodyBuffer.append((char) c);
          inputStream.close();

          // Display response
          body = bodyBuffer.toString();

          if (log.isDebug())
            log.logDebug(toString(), Messages.getString("HTTPPOST.Log.ResponseBody", body));
        }
        // return new Value(meta.getFieldName(), body);
        return RowDataUtil.addValueData(rowData, data.inputRowMeta.size(), body);
      } finally {
        if (inputStream != null) inputStream.close();
        // Release current connection to the connection pool once you are done
        post.releaseConnection();
      }
    } catch (Exception e) {
      throw new KettleException(
          Messages.getString("HTTPPOST.Error.CanNotReadURL", data.realUrl), e);
    }
  }