List of usage examples for org.apache.commons.httpclient.methods PostMethod addParameter
public void addParameter(NameValuePair paramNameValuePair) throws IllegalArgumentException
From source file:com.zb.app.biz.service.WeixinTest.java
public boolean sendMessage(String fakeid, String content) { if (isLogin) { PostMethod post = new PostMethod(sendUrl); post.setRequestHeader("Cookie", this.cookiestr); post.setRequestHeader("Host", "mp.weixin.qq.com"); post.setRequestHeader("Referer", "https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&token=" + token + "&tofakeid=" + fakeid + "&lang=zh_CN"); post.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); post.addParameter(new NameValuePair("type", "1")); post.addParameter(new NameValuePair("content", content)); post.addParameter(new NameValuePair("error", "false")); post.addParameter(new NameValuePair("imgcode", "")); post.addParameter(new NameValuePair("tofakeid", "681435581")); post.addParameter(new NameValuePair("token", token)); post.addParameter(new NameValuePair("ajax", "1")); try {//from w ww . ja v a2s. com int code = httpClient.executeMethod(post); if (HttpStatus.SC_OK == code) { System.out.println(post.getResponseBodyAsString()); } } catch (Exception e) { e.printStackTrace(); } } else { login(); sendMessage(fakeid, content); } return false; }
From source file:com.zb.app.biz.service.WeixinTest.java
private boolean compareFakeid(String fakeid, String openid) { PostMethod post = new PostMethod( "https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&token=" + token + "&tofakeid=" + fakeid + "&lang=zh_CN"); post.setRequestHeader("Cookie", this.cookiestr); post.setRequestHeader("Host", "mp.weixin.qq.com"); post.setRequestHeader("Referer", "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&token=" + token + "&lang=zh_CN&pagesize=10&pageidx=0&type=0"); post.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); post.addParameter(new NameValuePair("token", token)); post.addParameter(new NameValuePair("ajax", "1")); try {// w w w . j a v a 2 s . c om int code = httpClient.executeMethod(post); if (HttpStatus.SC_OK == code) { String str = post.getResponseBodyAsString(); String msgJson = StringUtils.substringBetween(str, "<script id=\"json-msgList\" type=\"json\">", "</script>"); JSONParser parser = new JSONParser(); try { JSONArray array = (JSONArray) parser.parse(msgJson); for (int i = 0; i < array.size(); i++) { JSONObject obj = (JSONObject) array.get(i); String content = (String) obj.get("content"); if (content.contains(openid)) { return true; } } } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } return false; }
From source file:org.apache.excalibur.source.factories.HTTPClientSource.java
/** * Factory method to create a new {@link PostMethod} with the given * {@link SourceParameters} object./*from w ww .j av a 2 s .c o m*/ * * @param uri URI * @param params {@link SourceParameters} * @return a {@link PostMethod} instance */ private PostMethod createPostMethod(final String uri, final SourceParameters params) { final PostMethod post = new PostMethod(uri); if (params == null) { return post; } for (final Iterator names = params.getParameterNames(); names.hasNext();) { final String name = (String) names.next(); for (final Iterator values = params.getParameterValues(name); values.hasNext();) { final String value = (String) values.next(); post.addParameter(new NameValuePair(name, value)); } } return post; }
From source file:org.apache.forrest.solr.client.SolrSearchGenerator.java
private PostMethod preparePost() { PostMethod filePost = new PostMethod(destination); filePost.addRequestHeader("User-Agent", AGENT); Iterator keys = map.keySet().iterator(); HashSet set = new HashSet(); while (keys.hasNext()) { String element = (String) keys.next(); if (!QUERY_PARAM.equals(element)) { String value = (String) map.get(element); set.add(new NameValuePair(element, value)); }//from www. j av a 2s . c o m } //make sure we send the query (even if null) to get a response set.add(new NameValuePair(QUERY_PARAM, query)); for (Iterator iter = set.iterator(); iter.hasNext();) { filePost.addParameter((NameValuePair) iter.next()); } return filePost; }
From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.post.PostServletOutputContentTypeTest.java
public void testJsonContentTypeException() throws Exception { // Perform a POST that fails: invalid PostServlet operation // with Accept header set to JSON final String url = HTTP_BASE_URL + MY_TEST_PATH; final PostMethod post = new PostMethod(url); post.setFollowRedirects(false);/*from w ww .j a va 2 s .c o m*/ post.addParameter(new NameValuePair(SlingPostConstants.RP_OPERATION, "InvalidTestOperationFor" + getClass().getSimpleName())); post.addRequestHeader("Accept", CONTENT_TYPE_JSON); final int status = httpClient.executeMethod(post); assertEquals(500, status); final String contentType = post.getResponseHeader("Content-Type").getValue(); final String expected = CONTENT_TYPE_JSON; assertTrue("Expecting content-type " + expected + " for failed POST request, got " + contentType, contentType != null && contentType.startsWith(expected)); }
From source file:org.apache.solr.explorer.server.SolrProxyServlet.java
@Override protected void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException { logger.info("Incoming request: " + req.getRequestURL()); String url = req.getHeader(TARGET_URL_PARAM); if (url == null) { throw new RuntimeException( "A target url must be specified as a parameter named '" + TARGET_URL_PARAM + "'"); }/*w w w . j av a2 s . c o m*/ rsp.setContentType("text/plain; charset=utf-8"); PostMethod post = new PostMethod(url); List<NameValuePair> params = extractAllParams(req); for (NameValuePair param : params) { post.addParameter(param); } try { int code = client.executeMethod(post); if (code != 200) { throw new RuntimeException("Bad respond from server (status: " + code + ")"); } InputStream input = post.getResponseBodyAsStream(); IOUtils.copy(input, rsp.getOutputStream()); } finally { // making sure the connection is released back to the connection manager post.releaseConnection(); } }
From source file:org.eclipse.mylyn.internal.monitor.usage.UsageUploadManager.java
public int getNewUid(StudyParameters studyParameters, IProgressMonitor monitor) throws UsageDataException { // TODO extract url for servlet String url = studyParameters.getUserIdServletUrl(); final PostMethod getUserIdMethod = new PostMethod(url); try {//www . ja v a2s . co m getUserIdMethod.addParameter(new NameValuePair("MylarUserID", "")); //$NON-NLS-1$//$NON-NLS-2$ AbstractWebLocation location = new WebLocation(url); HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(httpClient, location, monitor); final int status = WebUtil.execute(httpClient, hostConfiguration, getUserIdMethod, monitor); if (status == HttpStatus.SC_OK) { InputStream inputStream = WebUtil.getResponseBodyAsStream(getUserIdMethod, monitor); try { byte[] buffer = new byte[SIZE_OF_INT]; int numBytesRead = inputStream.read(buffer); int uid = new Integer(new String(buffer, 0, numBytesRead)).intValue(); return uid; } finally { inputStream.close(); } } else { throw new UsageDataException( NLS.bind(Messages.UsageUploadManager_Error_Getting_Uid_Http_Response, status)); } } catch (final IOException e) { if (e instanceof NoRouteToHostException || e instanceof UnknownHostException) { throw new UsageDataException(Messages.UsageUploadManager_Error_Getting_Uid_No_Network, e); } else { throw new UsageDataException( NLS.bind(Messages.UsageUploadManager_Error_Getting_Uid_X, e.getClass().getCanonicalName()), e); } } finally { WebUtil.releaseConnection(getUserIdMethod, monitor); } }
From source file:org.lnicholls.galleon.data.Users.java
public static synchronized boolean login(DataConfiguration dataConfiguration) throws Exception { log.debug("login: "); if (dataConfiguration.isConfigured()) {/*from w ww . j a v a 2 s . co m*/ String protocol = "https"; if (HOST.equals("localhost")) protocol = "http"; protocol = "http"; PostMethod post = new PostMethod(protocol + "://" + HOST + "/galleon/login.php"); post.setFollowRedirects(false); NameValuePair username = new NameValuePair("username", Tools.decrypt(dataConfiguration.getUsername())); NameValuePair password = new NameValuePair("password", Tools.decrypt(dataConfiguration.getPassword())); post.addParameter(username); post.addParameter(password); //log.debug(post.getQueryString()); try { if (httpclient.executeMethod(post) == 200) { String strGetResponseBody = post.getResponseBodyAsString(); log.debug(strGetResponseBody); SAXReader saxReader = new SAXReader(); StringReader stringReader = new StringReader(strGetResponseBody); Document document = saxReader.read(stringReader); Element root = document.getRootElement(); Element error = root.element("error"); if (error != null) { String code = Tools.getAttribute(error, "code"); String reason = Tools.getAttribute(error, "reason"); throw new GalleonException(reason, Integer.parseInt(code)); } if (root.elements().size() > 0) { throw new GalleonException("Server error"); } return true; } } catch (GalleonException ex) { log.error("Could not login", ex); throw ex; } catch (Exception ex) { log.error("Could not login", ex); throw new GalleonException("Server error"); } finally { post.releaseConnection(); } } return false; }
From source file:org.lnicholls.galleon.data.Users.java
private static synchronized boolean update(DataConfiguration dataConfiguration, String payload, String url) throws Exception { log.debug("update: "); if (dataConfiguration.isConfigured()) {//from w w w . j a va2 s . co m PostMethod post = new PostMethod("http://" + HOST + url); post.setFollowRedirects(false); try { if (login(dataConfiguration)) { log.debug(payload); NameValuePair data = new NameValuePair("data", payload); post.addParameter(data); if (httpclient.executeMethod(post) == 200) { String strGetResponseBody = post.getResponseBodyAsString(); log.debug(strGetResponseBody); SAXReader saxReader = new SAXReader(); StringReader stringReader = new StringReader(strGetResponseBody); Document document = saxReader.read(stringReader); Element root = document.getRootElement(); Element error = root.element("error"); if (error != null) { String code = Tools.getAttribute(error, "code"); String reason = Tools.getAttribute(error, "reason"); throw new GalleonException(reason, Integer.parseInt(code)); } if (root.elements().size() > 0) { throw new GalleonException("Server error"); } /* for (Iterator i = root.elementIterator(); i.hasNext();) { Element element = (Element) i.next(); System.out.println("element=" + element); } */ return true; } } } catch (GalleonException ex) { log.error("Could not update user", ex); throw ex; } catch (Exception ex) { log.error("Could not contact server", ex); throw new GalleonException("Server error"); } finally { post.releaseConnection(); logout(dataConfiguration); } } return false; }
From source file:org.lnicholls.galleon.data.Users.java
private static synchronized String retrieve(DataConfiguration dataConfiguration, String payload, String url) throws Exception { log.debug("retrieve: "); if (dataConfiguration.isConfigured()) {//from ww w . j a v a 2s. c om PostMethod post = new PostMethod("http://" + HOST + url); post.setFollowRedirects(false); try { if (login(dataConfiguration)) { log.debug(payload); if (payload != null) { NameValuePair data = new NameValuePair("data", payload); post.addParameter(data); } if (httpclient.executeMethod(post) == 200) { String strGetResponseBody = post.getResponseBodyAsString(); log.debug(strGetResponseBody); SAXReader saxReader = new SAXReader(); StringReader stringReader = new StringReader(strGetResponseBody); Document document = saxReader.read(stringReader); Element root = document.getRootElement(); Element error = root.element("error"); if (error != null) { String code = Tools.getAttribute(error, "code"); String reason = Tools.getAttribute(error, "reason"); throw new GalleonException(reason, Integer.parseInt(code)); } return strGetResponseBody; } } } catch (GalleonException ex) { log.error("Could not retrieve", ex); throw ex; } catch (Exception ex) { log.error("Could not contact server", ex); throw new GalleonException("Server error"); } finally { post.releaseConnection(); logout(dataConfiguration); } } return null; }