How to build a http post request with the correct entity with Java and not using any library?

孤街浪徒 提交于 2019-11-29 08:47:33

It is a sample code using HttpClient.

I hope this piece of information will be of help to you.

// 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();
}

You can use "GDataAPI" and "Guava-libraries".

PicasawebService myService
    = new PicasawebService("exampleCo-exampleApp-1"); // just id
myService.setUserCredentials(
    "liz@gmail.com", "mypassword"); // your mailaddress, password

// change "username", "albumid" and "photoid"
URL feedUrl = new URL(
    "https://picasaweb.google.com/data/feed/api/"
    + "user/username/albumid/albumid/photoid/photoid"); 

CommentEntry myComment = new CommentEntry(); 
myComment.setContent(
    new PlainTextConstruct("great photo!")); // there is comment
myService.insert(feedUrl, myComment);

Refere to following URL.

  1. http://code.google.com/intl/ja/apis/picasaweb/docs/2.0/developers_guide_java.html
  2. http://code.google.com/p/gdata-java-client/downloads (GDataAPI Download)
  3. http://code.google.com/p/guava-libraries/ (Guava-libraries)

You can use HttpClient from apache httpcomponents to create http requests.

Find the tutorials here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!