Java: Unterschied zwischen den Versionen
Aus wiki
(→Code Snippets) |
(→Code Snippets) |
||
| Zeile 23: | Zeile 23: | ||
== URL Parsing == | == URL Parsing == | ||
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8"); | List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8"); | ||
| + | |||
| + | == InputStream Reader == | ||
| + | int c; | ||
| + | StringBuilder responseJsonString = new StringBuilder(); | ||
| + | int len = connection.getContentLength(); | ||
| + | System.out.println("Content-Length: " + len); | ||
| + | if (len > 0) { | ||
| + | InputStream input = connection.getInputStream(); | ||
| + | int i = len; | ||
| + | while (((c = input.read()) != -1) && (-i > 0)) { | ||
| + | System.out.print((char) c); | ||
| + | responseJsonString.append((char) c); | ||
| + | } | ||
| + | input.close(); | ||
| + | } else { | ||
| + | |||
| + | |||
| + | } | ||
Version vom 4. August 2014, 08:54 Uhr
Inhaltsverzeichnis
Code Snippets
Regex
Pattern Matching
Named Matching
<syntaxhighlight lang="java">
private @Nullable String getToken(@Nonnull String responseURL) {</pre>
String patternString = "http://www.example.com/example#(token%3D(?<TOKEN>.+))%26otherstuff(.+%)";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(responseURL);
if(matcher.find()){
return matcher.group("TOKEN");
}
return null;
}
</syntaxhighlight>
Encoding
Base64 Encoding (Basic auth)
String base64encodedUsernameAndPassword = DatatypeConverter.printBase64Binary((username + ":" + password).getBytes());
webClient.addRequestHeader("Authorization", "Basic " + base64encodedUsernameAndPassword);
URL Parsing
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
InputStream Reader
int c;
StringBuilder responseJsonString = new StringBuilder();
int len = connection.getContentLength();
System.out.println("Content-Length: " + len);
if (len > 0) {
InputStream input = connection.getInputStream();
int i = len;
while (((c = input.read()) != -1) && (-i > 0)) {
System.out.print((char) c);
responseJsonString.append((char) c);
}
input.close();
} else {
}