Rest Client XML
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.CloseableHttpClient;
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("API_URL");
String json = "XML_CONTENT";
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/xml");
httpPost.setHeader("Content-type", "application/xml");
UsernamePasswordCredentials creds
= new UsernamePasswordCredentials("USER_NAME", "PWD");
httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
CloseableHttpResponse response = client.execute(httpPost);
String responseString;
responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(responseString);
// assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
client.close();
No comments:
Post a Comment