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

后端 未结 3 1369
猫巷女王i
猫巷女王i 2020-12-18 14:26

How should I build the entity to achieve this post request?

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

<         


        
3条回答
  •  死守一世寂寞
    2020-12-18 15:01

    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 = 
        ""
        + "" + comment + ""
        + ""
        + "";
    
    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();
    }
    

提交回复
热议问题