I'm having trouble doing an HttpPut in Java. Getting a "415 Unsupported Media Type" error.
Here's my put method:
public HttpEntity put(String path, String xml) throws Exception
{
// Use HTTPS
HttpHost targetHost = new HttpHost("api.constantcontact.com", 443, "https");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(),
targetHost.getPort()), new UsernamePasswordCredentials(apikey+"%"+username, password));
// Use BASIC authentication
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
// Put the request
HttpPut httpput = new HttpPut("/ws/customers/"+username+path);
httpput.addHeader("Content-Type", "application/atom+xml");
StringEntity entity = new StringEntity(xml, "UTF-8");
entity.setContentType("application/xml");
httpput.setEntity(entity);
HttpResponse response = httpclient.execute(targetHost, httpput, localcontext);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() != HttpStatus.SC_OK)
{
throw new Exception(status.getStatusCode()+" "+status.getReasonPhrase());
}
return response.getEntity();
}
Here's the XML entity:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text"> </title>
<updated>2008-07-23T14:21:06.407Z</updated>
<author/><id>data:,none</id>
<summary type="text">Contact</summary>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/">
<EmailAddress>nobody@nowhere.com</EmailAddress>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/.../lists/0"/>
</ContactLists>
</Contact>
</content>
</entry>
Thanks.
Tom
... View more