T
Tami91
Neues Mitglied
- 0
Hallo ich versuche eine Http-Request an einen Apache Tomcat Server zu versenden.
Er soll die Form (siehe unten) haben:
Headers:
Header 1, Header 2
Body:
<authenticateRequest>
<userName>username</userName>
<Password>password</Password>
<ipAddress></ipAddress>
</authenticateRequest>
Hier mein Code:
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost p = new HttpPost(url);
JSONObject object = new JSONObject();
try {
object.put("userName", Name);
object.put("password", Password);
} catch (Exception ex) {
//Nix
}
String message = object.toString();
p.setEntity(new StringEntity(message, "UTF8"));
p.addHeader("Content-Type", "application/xml");
p.addHeader("Accept", "application/json");
HttpResponse response = httpclient.execute(p);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
String jaxrsmessage = read(instream);
System.out.println(jaxrsmessage);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
private static String read(InputStream instream) {
StringBuilder sb = null;
try {
sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(
instream));
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
}
instream.close();
} catch (IOException e) {
}
return sb.toString();
}
Wie ihr sehen könnt habe ich die Header, Username und Password hinzugefügt. Aber wie bekomme ich das <authenticateRequest> um meine Bodyobjekte?
Und wird der Client in dieser Form so funktionieren wie ich es mir vorstelle?
Er soll die Form (siehe unten) haben:
Headers:
Header 1, Header 2
Body:
<authenticateRequest>
<userName>username</userName>
<Password>password</Password>
<ipAddress></ipAddress>
</authenticateRequest>
Hier mein Code:
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost p = new HttpPost(url);
JSONObject object = new JSONObject();
try {
object.put("userName", Name);
object.put("password", Password);
} catch (Exception ex) {
//Nix
}
String message = object.toString();
p.setEntity(new StringEntity(message, "UTF8"));
p.addHeader("Content-Type", "application/xml");
p.addHeader("Accept", "application/json");
HttpResponse response = httpclient.execute(p);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
String jaxrsmessage = read(instream);
System.out.println(jaxrsmessage);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
private static String read(InputStream instream) {
StringBuilder sb = null;
try {
sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(
instream));
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
}
instream.close();
} catch (IOException e) {
}
return sb.toString();
}
Wie ihr sehen könnt habe ich die Header, Username und Password hinzugefügt. Aber wie bekomme ich das <authenticateRequest> um meine Bodyobjekte?
Und wird der Client in dieser Form so funktionieren wie ich es mir vorstelle?