webrequester.CouchDBWebRequest.java Source code

Java tutorial

Introduction

Here is the source code for webrequester.CouchDBWebRequest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package webrequester;

import java.io.IOException;
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import static org.apache.http.HttpVersion.HTTP;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author loic
 */
public class CouchDBWebRequest extends AbstractWebRequest {
    public CouchDBWebRequest() {
        super("http", "localhost", "/lignes", 5984);

    }

    public String requestWithPut(String content) {
        String s = "";
        try {

            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPut httpput = new HttpPut(buildURI().toString());

            StringEntity se = new StringEntity(content);
            httpput.setEntity(se);

            CloseableHttpResponse response = httpclient.execute(httpput);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    s = EntityUtils.toString(entity, "UTF-8");
                }
            } finally {
                response.close();
            }
        } catch (IOException ex) {
            return null;
        }
        return s;
    }
}