List of usage examples for org.apache.commons.httpclient NameValuePair NameValuePair
public NameValuePair(String name, String value)
From source file:com.leosys.core.utils.SendMessage.java
public static String postMessage(String phoneNo, String sendText) throws IOException { HttpClient client = new HttpClient(); PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/"); post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// ? NameValuePair[] data = { new NameValuePair("Uid", "fanyy"), // ?? new NameValuePair("Key", "694de3e5ca9f7015eaef"), // ??, new NameValuePair("smsMob", phoneNo), // ?? new NameValuePair("smsText", sendText + "??") };// post.setRequestBody(data);// w ww .jav a2s.c om client.executeMethod(post); Header[] headers = post.getResponseHeaders(); int statusCode = post.getStatusCode(); System.out.println("statusCode:" + statusCode); for (Header h : headers) { System.out.println(h.toString()); } String result = new String(post.getResponseBodyAsString().getBytes("gbk")); System.out.println(result); post.releaseConnection(); return result; }
From source file:codeOrchestra.lcs.license.plimus.PlimusHelper.java
private static PlimusResponse executePlimusAction(String key, PlimusValidationAction action) throws IOException { GetMethod getMethod = new GetMethod(VALIDATION_URL); getMethod.getParams().setParameter("http.socket.timeout", new Integer(TIMEOUT)); getMethod.setQueryString(new NameValuePair[] { new NameValuePair("action", action.name()), new NameValuePair("productId", PRODUCT_ID), new NameValuePair("key", key) }); httpClient.executeMethod(getMethod); return new PlimusResponse(getMethod.getResponseBodyAsString()); }
From source file:com.cloudapi.cloud.rootadminhost.HostResponse.java
public Document id(String id) throws Exception { LinkedList<NameValuePair> arguments = newQueryValues("id", null); arguments.add(new NameValuePair("id", id)); return Request(arguments); }
From source file:com.thoughtworks.twist.mingle.core.MingleUtils.java
public static String getQueryUrlAfterEncodingURL(String fullUrl) throws UnsupportedEncodingException { String queryUrl = fullUrl.substring(fullUrl.indexOf('?') + 1); String[] split = queryUrl.split("&"); ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(); for (String nameValuePair : split) { if (nameValuePair.contains("=")) { int indexOfEquals = nameValuePair.indexOf('='); String name = nameValuePair.substring(0, indexOfEquals); String value = nameValuePair.substring(indexOfEquals + 1); name = URLDecoder.decode(name, "utf-8"); value = URLDecoder.decode(value, "utf-8"); pairs.add(new NameValuePair(name, value)); }/*from w w w . j av a 2 s . co m*/ } String formUrlEncode = EncodingUtil.formUrlEncode(pairs.toArray(new NameValuePair[0]), "utf-8"); return formUrlEncode; }
From source file:ClientPost.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod("http://localhost:8080/home/viewPost.jsp"); NameValuePair[] postData = { new NameValuePair("username", "devgal"), new NameValuePair("department", "development"), new NameValuePair("email", "devgal@yahoo.com") }; //the 2.0 beta1 version has a // PostMethod.setRequestBody(NameValuePair[]) //method, as addParameters is deprecated postMethod.addParameters(postData);/*from w w w. j a v a 2 s . co m*/ httpClient.executeMethod(postMethod); //display the response to the POST method response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); //A "200 OK" HTTP Status Code if (postMethod.getStatusCode() == HttpStatus.SC_OK) { out.println(postMethod.getResponseBodyAsString()); } else { out.println("The POST action raised an error: " + postMethod.getStatusLine()); } //release the connection used by the method postMethod.releaseConnection(); }
From source file:com.feilong.tools.net.httpclient3.NameValuePairUtil.java
/** * map??NameValuePair.//from www .j ava 2 s.c o m * * @param params * the params * @return if (Validator.isNotNullOrEmpty(params)), will return null */ public static NameValuePair[] fromMap(Map<String, String> params) { if (Validator.isNotNullOrEmpty(params)) { NameValuePair[] nameValuePairs = new NameValuePair[params.size()]; int i = 0; for (Map.Entry<String, String> entry : params.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); nameValuePairs[i] = new NameValuePair(key, value); i++; } return nameValuePairs; } return null; }
From source file:com.dotmarketing.util.UpdateUtil.java
/** * @return the new version if found. Null if up to date. * @throws DotDataException if an error is encountered *//*from w w w .j ava2 s .co m*/ public static String getNewVersion() throws DotDataException { //Loading the update url Properties props = loadUpdateProperties(); String fileUrl = props.getProperty(Constants.PROPERTY_UPDATE_FILE_UPDATE_URL, ""); Map<String, String> pars = new HashMap<String, String>(); pars.put("version", ReleaseInfo.getVersion()); //pars.put("minor", ReleaseInfo.getBuildNumber() + ""); pars.put("check_version", "true"); pars.put("level", System.getProperty("dotcms_level")); if (System.getProperty("dotcms_license_serial") != null) { pars.put("license", System.getProperty("dotcms_license_serial")); } HttpClient client = new HttpClient(); PostMethod method = new PostMethod(fileUrl); Object[] keys = (Object[]) pars.keySet().toArray(); NameValuePair[] data = new NameValuePair[keys.length]; for (int i = 0; i < keys.length; i++) { String key = (String) keys[i]; NameValuePair pair = new NameValuePair(key, pars.get(key)); data[i] = pair; } method.setRequestBody(data); String ret = null; try { client.executeMethod(method); int retCode = method.getStatusCode(); if (retCode == 204) { Logger.info(UpdateUtil.class, "No new updates found"); } else { if (retCode == 200) { String newMinor = method.getResponseHeader("Minor-Version").getValue(); String newPrettyName = null; if (method.getResponseHeader("Pretty-Name") != null) { newPrettyName = method.getResponseHeader("Pretty-Name").getValue(); } if (newPrettyName == null) { Logger.info(UpdateUtil.class, "New Version: " + newMinor); ret = newMinor; } else { Logger.info(UpdateUtil.class, "New Version: " + newPrettyName + "/" + newMinor); ret = newPrettyName; } } else { throw new DotDataException( "Unknown return code: " + method.getStatusCode() + " (" + method.getStatusText() + ")"); } } } catch (HttpException e) { Logger.error(UpdateUtil.class, "HttpException: " + e.getMessage(), e); throw new DotDataException("HttpException: " + e.getMessage(), e); } catch (IOException e) { Logger.error(UpdateUtil.class, "IOException: " + e.getMessage(), e); throw new DotDataException("IOException: " + e.getMessage(), e); } return ret; }
From source file:com.github.jobs.api.GithubJobsApi.java
public static List<Job> search(Search search) { ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(); if (search.getSearch() != null) { pairs.add(new NameValuePair(ApiConstants.SEARCH, search.getSearch())); }//from w ww .j av a 2 s . c o m if (search.getLocation() != null) { pairs.add(new NameValuePair(ApiConstants.LOCATION, search.getLocation())); } else if (search.getLatitude() != 0 && search.getLongitude() != 0) { pairs.add(new NameValuePair(ApiConstants.LATITUDE, String.valueOf(search.getLatitude()))); pairs.add(new NameValuePair(ApiConstants.LONGITUDE, String.valueOf(search.getLongitude()))); } if (search.getPage() > 0) { pairs.add(new NameValuePair(ApiConstants.PAGE, String.valueOf(search.getPage()))); } if (search.isFullTime()) { pairs.add(new NameValuePair(ApiConstants.FULL_TIME, String.valueOf(search.isFullTime()))); } try { String url = createUrl(ApiConstants.POSITIONS_URL, pairs); String response = HttpHandler.getInstance().getRequest(url); if (response == null) { throw new RuntimeException("Error calling API; it returned null."); } // convert json to object Gson gson = new Gson(); JSONArray jsonArray = new JSONArray(response); List<Job> jobs = new ArrayList<Job>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject object = jsonArray.getJSONObject(i); jobs.add(gson.fromJson(object.toString(), Job.class)); } return jobs; } catch (URIException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return null; }
From source file:com.touch6.sm.gateway.webchinese.Webchinese.java
public static String batchSend(String url, String uid, String key, String phone, String msg, String contentType, String charset) throws CoreException { HttpClient client = new HttpClient(); PostMethod post = new PostMethod(url); post.addRequestHeader("Content-Type", contentType);//? NameValuePair[] data = { new NameValuePair("Uid", uid), new NameValuePair("Key", key), new NameValuePair("smsMob", phone), new NameValuePair("smsText", msg) }; post.setRequestBody(data);/*from w w w. j a v a2s .com*/ try { client.executeMethod(post); } catch (Exception e) { logger.info("??:", e); throw new CoreException(ECodeUtil.getCommError(SystemErrorConstant.SYSTEM_EXCEPTION)); } Header[] headers = post.getResponseHeaders(); int statusCode = post.getStatusCode(); System.out.println("statusCode:" + statusCode); for (Header h : headers) { System.out.println(h.toString()); } String result; try { result = new String(post.getResponseBodyAsString().getBytes(charset)); System.out.println(result); //??? } catch (Exception e) { logger.info("??:", e); throw new CoreException(ECodeUtil.getCommError(SystemErrorConstant.SYSTEM_EXCEPTION)); } post.releaseConnection(); return result; }
From source file:com.legendshop.central.license.HttpClientLicenseHelper.java
public String postMethod(String paramString) { String str = null;//from w w w. ja va 2 s . c o m HttpClient localHttpClient = new HttpClient(); PostMethod localPostMethod = new PostMethod(this._$1); NameValuePair[] arrayOfNameValuePair = new NameValuePair[1]; arrayOfNameValuePair[0] = new NameValuePair("_ENTITY", paramString); localPostMethod.addParameters(arrayOfNameValuePair); try { localHttpClient.executeMethod(localPostMethod); str = localPostMethod.getResponseBodyAsString(); } catch (Exception localException) { } finally { localPostMethod.releaseConnection(); } return str; }