Refine search
PostMethod post = new PostMethod("http://jakarata.apache.org/"); NameValuePair[] data = { new NameValuePair("user", "joe"), new NameValuePair("password", "bloggs") }; post.setRequestBody(data); // execute method and handle any error responses. ... InputStream in = post.getResponseBodyAsStream(); // handle response.
/** * Return a name/value string suitable for sending in a <tt>"Cookie"</tt> * header as defined in RFC 2109 for backward compatibility with cookie * version 0 * @param buffer The string buffer to use for output * @param param The parameter. * @param version The cookie version */ private void formatParam(final StringBuffer buffer, final NameValuePair param, int version) { if (version < 1) { buffer.append(param.getName()); buffer.append("="); if (param.getValue() != null) { buffer.append(param.getValue()); } } else { this.formatter.format(buffer, param); } }
/** * Adds a new parameter to be used in the POST request body. * * @param param The parameter to add. * * @throws IllegalArgumentException if the argument is null or contains * null values * * @since 2.0 */ public void addParameter(NameValuePair param) throws IllegalArgumentException { LOG.trace("enter PostMethod.addParameter(NameValuePair)"); if (param == null) { throw new IllegalArgumentException("NameValuePair may not be null"); } addParameter(param.getName(), param.getValue()); }
throw new IllegalArgumentException("Attribute may not be null."); if (attribute.getName() == null) { throw new IllegalArgumentException("Attribute Name may not be null."); throw new IllegalArgumentException("Cookie may not be null."); final String paramName = attribute.getName().toLowerCase(); final String paramValue = attribute.getValue(); attribute.toString()); } else { handler.parse(cookie, paramValue);
public IdentityDetails readIdentity(String username, String token) throws IOException, SingleSignOnException { HttpClient httpClient = new HttpClient(); PostMethod post = new PostMethod(IDENT_READ_ENDPOINT); LOG.log(Level.INFO, "Obtaining identity: username: [" + username + "]: token: [" + token + "]: REST url: [" + IDENT_READ_ENDPOINT + "]"); NameValuePair[] data = { new NameValuePair("name", username), new NameValuePair("admin", token) }; post.setRequestBody(data); httpClient.executeMethod(post); if (post.getStatusCode() != HttpStatus.SC_OK) { throw new SingleSignOnException(post.getStatusLine().toString()); } return parseIdentityDetails(post.getResponseBodyAsString().trim()); }
@Override public void send() { HttpClient client = new HttpClient(); PostMethod post = new PostMethod("https://api.pushover.net/1/messages.json"); post.setRequestBody(new NameValuePair[]{ new NameValuePair("token", appToken), new NameValuePair("user", userToken), new NameValuePair("message", content + "\n\n" + extraMessage), new NameValuePair("title", title), new NameValuePair("priority", priority.toString()), new NameValuePair("url", url), new NameValuePair("url_title", urlTitle) }); try { client.executeMethod(post); } catch (IOException e) { LOGGER.severe("Error while sending notification: " + e.getMessage()); e.printStackTrace(); } }
private void doPostMethod(final String operation, final String arcName, final String arcUrl) throws IOException { PostMethod method = new PostMethod(serverUrl); NameValuePair[] data = { new NameValuePair(ResourceFileLocationDBServlet.OPERATION_ARGUMENT, operation), new NameValuePair(ResourceFileLocationDBServlet.NAME_ARGUMENT, arcName), new NameValuePair(ResourceFileLocationDBServlet.URL_ARGUMENT, arcUrl) }; method.setRequestBody(data); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new IOException("Method failed: " + method.getStatusLine()); } String responseString = method.getResponseBodyAsString(); if(!responseString.startsWith(OK_RESPONSE_PREFIX)) { throw new IOException(responseString); } }
PostMethod getJsonUrlRequest() { PostMethod request = new PostMethod(getJsonUrl(this.packId)); List<Part> parts = new ArrayList<Part>(); for (NameValuePair part : this.stringParams.values()) { parts.add(new StringPart(part.getName(), part.getValue(), getCharset().name())); } for (Part part : this.fileParams.values()) { parts.add(part); } request.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), request.getParams())); return request; }
public String post(String method, NameValuePair[] parameters) { PostMethod post = new PostMethod(getApiEndpoint() + method); post.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE); String auth_info = this.getEmail() + ":" + this.getApiKey(); String encoded_auth = new String(Base64.encodeBase64(auth_info.getBytes(encodingCharset)), encodingCharset); post.setRequestHeader("Authorization", "Basic " + encoded_auth); try { post.setRequestBody(EncodingUtil.formUrlEncode(parameters, Charset.defaultCharset().name())); HttpClient client = getClient(); client.executeMethod(post); String response = post.getResponseBodyAsString(); if (post.getStatusCode() != HttpStatus.SC_OK) { StringBuilder params = new StringBuilder(); for (NameValuePair pair: parameters) { params.append("\n").append(pair.getName()).append(":").append(pair.getValue()); } LOGGER.log(Level.SEVERE, "Error sending Zulip message:\n" + response + "\n\n" + "We sent:" + params.toString()); } return response; } catch (IOException e) { LOGGER.log(Level.SEVERE, "Error sending Zulip message: " + e.getMessage()); } finally { post.releaseConnection(); } return null; }
PostMethod method = new PostMethod(url); method.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1"); NameValuePair nvp1= new NameValuePair("firstName","fname"); NameValuePair nvp2= new NameValuePair("lastName","lname"); NameValuePair nvp3= new NameValuePair("email","email@email.com"); method.setQueryString(new NameValuePair[]{nvp1,nvp2,nvp3});
/** * 发送一个post请求 * * @param url 地址 * @param params 参数 * @return 返回结果 * @throws java.io.IOException */ public static String post(String url, Map<String, String> params) throws IOException { PostMethod method = new PostMethod(url); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, DEFAULT_CHARSET); method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, DEFAULT_TIMEOUT); if (MapUtils.isNotEmpty(params)) { List<NameValuePair> pairs = new ArrayList<>(params.size()); for (Map.Entry<String, String> entry : params.entrySet()) { NameValuePair pair = new NameValuePair(); pair.setName(entry.getKey()); pair.setValue(entry.getValue()); pairs.add(pair); } method.addParameters(pairs.toArray(new NameValuePair[pairs.size()])); } return executeMethod(method); }
@Test public void addHttpPostParams() { //given ErrorReportSender sender = new ErrorReportSender(); Map<String, String> values = new HashMap<>(); values.put("key1", "value1"); values.put("key2", "value2"); PostMethod method = new PostMethod(); //when sender.addHttpPostParams(values, method); //then assertEquals("value1", method.getParameter("key1").getValue()); assertEquals("value2", method.getParameter("key2").getValue()); }
PostMethod post = new PostMethod("http://localhost:3000/projects"); NameValuePair[] data = { new NameValuePair("project[name]", "from java"), new NameValuePair("project[life_cycle_id]", "5") }; post.setRequestBody(data); // execute method and handle any error responses. new HttpClient().executeMethod(post);
GetMethod method = new GetMethod("example.com/page"); method.setQueryString(new NameValuePair[] { new NameValuePair("key", "value") });
PostMethod getPOSTMethod(String servicePath, List<WebscriptParam> params) { PostMethod postMethod = new PostMethod(this.baseUrl + servicePath); if (params != null) { List<NameValuePair> args = new ArrayList<NameValuePair>(); for (WebscriptParam param : params) { args.add(new NameValuePair(param.getName(), param.getValue())); } postMethod.addParameters(args.toArray(new NameValuePair[args.size()])); } return postMethod; }
private static NameValuePair[] createNameValuePairArrayFromQuery(String query) { // example: // csarID=Moodle.csar&serviceTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}Moodle&nodeTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}VmApache System.out.println("Splitting query: " + query); String[] pairs = query.trim().split("&"); NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length]; int count = 0; for (String pair : pairs) { System.out.println("Splitting query pair: " + pair); String[] keyValue = pair.split("="); NameValuePair nameValuePair = new NameValuePair(); System.out.println("Key: " + keyValue[0] + " Value: " + keyValue[1]); nameValuePair.setName(keyValue[0]); nameValuePair.setValue(keyValue[1]); nameValuePairArray[count] = nameValuePair; count++; } return nameValuePairArray; }
params.add(new NameValuePair("username", uname)); params.add(new NameValuePair("realm", realm)); params.add(new NameValuePair("nonce", nonce)); params.add(new NameValuePair("uri", uri)); params.add(new NameValuePair("response", response)); params.add(new NameValuePair("qop", getQopVariantString())); params.add(new NameValuePair("nc", NC)); params.add(new NameValuePair("cnonce", this.cnonce)); params.add(new NameValuePair("algorithm", algorithm)); params.add(new NameValuePair("opaque", opaque)); buffer.append(", "); boolean noQuotes = "nc".equals(param.getName()) || "qop".equals(param.getName()); this.formatter.setAlwaysUseQuotes(!noQuotes); this.formatter.format(buffer, param);
this.charset = charsetPair.getValue(); } else if (charset != null && charsetPair == null) {
/** * @return */ public boolean hasResponse() { NameValuePair pair = postMethod.getResponseHeader("Content-Type"); if(pair == null) return false; String ct = pair.getValue(); return ct != null && ct.length() > 0; }
public String getHTTPMethod() { return postMethod.getParameter("http.protocol.version").getValue(); }