6 messages in com.googlegroups.bloggerdevRe: [bloggerDev] Posting to Blogger u...| From | Sent On | Attachments |
|---|---|---|
| Justin | 23 Mar 2006 09:05 | |
| Scott Wilson | 23 Mar 2006 11:23 | |
| Justin | 24 Mar 2006 09:44 | |
| alex...@gmail.com | 24 Mar 2006 13:28 | |
| Scott Wilson | 27 Mar 2006 07:31 | |
| jgoe...@gmail.com | 07 Apr 2006 08:56 |
| Subject: | Re: [bloggerDev] Posting to Blogger using Java![]() |
|---|---|
| From: | Scott Wilson (scot...@gmail.com) |
| Date: | 03/23/2006 11:23:32 AM |
| List: | com.googlegroups.bloggerdev |
Hi Justin,
I don't know offhand what the problem is, but here's a snippet from our app that we use for a range of Atom providers - I hope it helps.
- Scott
(nb, we make an entry as a Document using JDom and pass it to this method)
/** * Connect to Atom service and send the post */ private void doPost(Document entry) throws PlexException { IPlexConduitConfiguration config = getConfiguration(); if(config == null) { return; }
try { String data = XMLUtils.write2XMLString(entry);
String location = config.getLocation(); String userName = config.getUserName(); String password = config.getPassword();
Authenticator.setDefault(new BasicAuthenticator()); URL url = new URL(location); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(getAuthenticationMethod().equals (IPlexConduit.WS_SECURITY)) { conn.addRequestProperty("X-WSSE", SecurityHelper.getWSSEHeader(userName, password)); }
// Secure connection if(conn instanceof HttpsURLConnection) { SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); ((HttpsURLConnection)conn).setSSLSocketFactory (factory); }
conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/atom +xml"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Plex 1.0)"); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter (conn.getOutputStream()); wr.write(data); wr.flush();
// Get the response. May be 200, 201, 202... int response = conn.getResponseCode(); if(response >= 300) { throw new PlexException("Server rejected post"); } wr.close(); conn.disconnect();
} catch(Exception e) { e.printStackTrace(); throw new PlexException("Couldn't connect to service"); } }
On 23 Mar 2006, at 18:05, Justin wrote:
Hello,
I am trying to post a new entry to my blog using Java. So far I have been able to authenticate with Blogger with Basic HTTP authentication and read blog entries, but I haven't been able to post anything. Why does the following code get an HTTP 500 response code back from the server, or what should I do that will work?
Justin
import java.io.*; import java.net.*; import java.security.MessageDigest; import java.text.SimpleDateFormat; import java.util.Date;
public class PostEntry {
public static void main(String[] args) { try {
URL url = new URL("https://www.blogger.com/atom/BLOGIDNUMBER");
String username = "name"; String password = "secret";
String userPass = username + ":" + password; String base64UserPass = base64Encode(userPass.getBytes());
URLConnection connection = url.openConnection();
connection.addRequestProperty("Authorization", "BASIC " + base64UserPass); ((HttpURLConnection)connection).setRequestMethod("POST"); connection.setDoOutput(true);
String message = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><entry xmlns=\"http://purl.org/atom/ns#\"><title mode=\"escaped\" type=\"text/plain\">atom test</title><issued>" + generateTimestamp() + "</issued><generator url=\"http://www.mysite.com\">title</generator><content type=\"application/xhtml+xml\"><div xmlns=\"http://www.w3.org/1999/xhtml\">testing the Atom API</div></content></entry>"; String encodedMessage = URLEncoder.encode(message, "UTF-8");
PrintWriter out = new PrintWriter(((HttpURLConnection)connection).getOutputStream()); out.println(encodedMessage);
} catch (Exception e) { e.printStackTrace(); } }
private static String base64Encode(byte[] bytes) { return new sun.misc.BASE64Encoder().encode(bytes); }
private static String generateTimestamp() { SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); return dateFormatter.format(new Date()); } }
/-/-/-/-/-/ Scott Wilson s.wi...@bangor.ac.uk http://www.cetis.ac.uk/members/scott FOAF: http://www.cetis.ac.uk/members/scott/foaf.rdf




