Mit Java und der Apache HttpClient-Bibliothek Version 4.1.1 habe ich einen Web-Client geschrieben, der eine „email“-Adresse als HTTP POST Parameter an eine PHP-Webseite übergibt, welche daraufhin die empfangene Email-Adresse in einer Textdatei speichert. Der Code dient nur als Beispiel.
HttpPostExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
public class HttpPostExample
{
public static void main(String[] args) throws Exception
{
String url = "http://localhost/test/post.php";
HttpParams params = new BasicHttpParams();
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("email", "bn@bennyn.de"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
} |
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
public class HttpPostExample
{
public static void main(String[] args) throws Exception
{
String url = "http://localhost/test/post.php";
HttpParams params = new BasicHttpParams();
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("email", "bn@bennyn.de"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
}
post.php
1
2
3
4
5
6
| <?php
$file = 'C:\\xampp\\htdocs\\test\\post.txt';
$post= $_POST['email'];
file_put_contents($file, $post);
?>
OK |
<?php
$file = 'C:\\xampp\\htdocs\\test\\post.txt';
$post= $_POST['email'];
file_put_contents($file, $post);
?>
OK
Etwas raffinierter und mit Fehlerabfrage (zum Verhindern von Notice: Undefined index: email
):
1
2
3
4
5
6
7
8
9
10
11
| <?php
$status = "OK";
$file = 'C:\\xampp\\htdocs\\test\\post.txt';
if (isset($_POST['email'])) {
$post = $_POST['email'];
file_put_contents($file, $post);
} else {
$status = "ERROR";
}
echo $status;
?> |
<?php
$status = "OK";
$file = 'C:\\xampp\\htdocs\\test\\post.txt';
if (isset($_POST['email'])) {
$post = $_POST['email'];
file_put_contents($file, $post);
} else {
$status = "ERROR";
}
echo $status;
?>