java - 如何使用 Java 构建具有正确实体的 http post 请求而不使用任何库?

标签 java http post httprequest picasa

我应该如何构建实体来实现这个发布请求?

POST https://picasaweb.google.com/data/feed/api/user/userID/albumid/albumID/photoid/photoID

<entry xmlns='http://www.w3.org/2005/Atom'>
  <content>great photo!</content>
  <category scheme="http://schemas.google.com/g/2005#kind"
    term="http://schemas.google.com/photos/2007#comment"/>
</entry>

来自: http://code.google.com/intl/zh-TW/apis/picasaweb/docs/2.0/developers_guide_protocol.html#AddComments

有人可以提供示例或任何提示吗? 非常感谢。

更新: 我在这里添加了我的代码:

        List<Header> headers = new ArrayList<Header>();
    headers.add(new BasicHeader("GData-Version", "2"));
    headers.add(new BasicHeader("Authorization", "GoogleLogin auth=" + mAuthToken));

    EntityTemplate entity = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream ostream) throws IOException {
            Writer writer = new OutputStreamWriter(ostream, "UTF-8");

            writer.write("\r\n");
            writer.write("<entry xmlns='http://www.w3.org/2005/Atom'>");
            writer.write("<content>" + comment + "</content>");
            writer.write("<category scheme=\"http://schemas.google.com/g/2005#kind\"\r\n");
            writer.write("term=\"http://schemas.google.com/photos/2007#comment\"/>");
            writer.write("</entry>\r\n");

            writer.flush();
        }
    });

仍然没有运气。有什么想法吗?

最佳答案

这是一个使用HttpClient 的示例代码。

希望以上信息对您有所帮助。

// yourID
String userID = "";
String albumID = "";
String photoID = "";

HttpPost postRequest = new HttpPost(
    "https://picasaweb.google.com/data/feed/api/user/" + userID
    + "/albumid/" + albumID + "/photoid/" + photoID);

postRequest.addHeader(new BasicHeader("GData-Version", "2.0"));
postRequest.addHeader(new BasicHeader("Authorization",
    "GoogleLogin auth=" + mAuthToken));

String content = 
    "<entry xmlns='http://www.w3.org/2005/Atom'>"
    + "<content>" + comment + "</content>"
    + "<category scheme='http://schemas.google.com/g/2005#kind'"
    + " term='http://schemas.google.com/photos/2007#comment'/>"
    + "</entry>";

try {
    StringEntity entity = new StringEntity(content);
    entity.setContentType(new BasicHeader("Content-Type",
        "application/atom+xml"));
    postRequest.setEntity(entity);

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(postRequest);

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

关于java - 如何使用 Java 构建具有正确实体的 http post 请求而不使用任何库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5101817/

相关文章:

python - 使用 python 请求在 HTTP POST 上规避错误 414

json - Flutter 迁移到 null 安全性,是迟到还是可为 null?

javascript - jQuery 帖子在 Chrome 中有效,但在 IE 或 FF 中无效

java - 如何测量JBOSS 7服务器的启动时间?

java - BigInteger 的 N 次方根

http - Asp.net Web API - 在哪里保存自定义处理程序?

http - Node.js - 控制 POST 请求的大小

python - Django View 中的空 request.POST

java - java 中的多线程 - 4 个线程自动执行相同的操作

java - 如何知道 Java 扫描器分隔符在哪里停止?