What if you create a new URL object for it, for example URL urlObject = new URL(url)
, then urlObject.getQuery()
and urlObject.getPath()
to separate it correctly, urlObject.getPath()
request passwords into a list or map, or whatever then do something like:
EDIT: I just found out that the HttpClient library has a URLEncodedUtils.parse()
method, which you can easily use with the code below. I will edit it to match, but not verified.
With Apache HttpClient, it will be something like:
URI urlObject = new URI(url,"UTF-8"); HttpClient httpclient = new DefaultHttpClient(); List<NameValuePair> formparams = URLEncodedUtils.parse(urlObject,"UTF-8"); UrlEncodedFormEntity entity; entity = new UrlEncodedFormEntity(formparams); HttpPost httppost = new HttpPost(urlObject.getPath()); httppost.setEntity(entity); httppost.addHeader("Content-Type","application/x-www-form-urlencoded"); HttpResponse response = httpclient.execute(httppost); HttpEntity entity2 = response.getEntity();
With Java URLConnection, it will be something like:
// Iterate over query params from urlObject.getQuery() like while(en.hasMoreElements()){ String paramName = (String)en.nextElement(); // Iterator over yourListOfKeys String paramValue = yourMapOfValues.get(paramName); // replace yourMapOfNameValues str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue); } try{ URL u = new URL(urlObject.getPath()); //here the url path from your urlObject URLConnection uc = u.openConnection(); uc.setDoOutput(true); uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); PrintWriter pw = new PrintWriter(uc.getOutputStream()); pw.println(str); pw.close(); BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())); String res = in.readLine(); in.close(); // ... }
source share