Newer
Older
talkCrawler / src / tool / http / Post.java
package tool.http;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.json.JsonArray;
import javax.json.JsonObject;
import talkcrawler.Article;
import talkcrawler.TalkCrawlerProperties;
import tool.json.JsonTool;

/**
 *
 * @author yuu
 */
public class Post {
    URL url;
    
    public Post(TalkCrawlerProperties prop) throws MalformedURLException, FileNotFoundException, IOException {
        this.url = new URL(prop.getProperty("SOLR_URL"));
    }
    
    @SuppressWarnings("UseSpecificCatch")
    public void post(JsonArray array) {
        String jsonText = array.toString();
        HttpURLConnection con = null;
        try {
            con = (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(60000);
            con.setReadTimeout(60000);
            con.addRequestProperty("Content-Type", "application/json; charset=UTF-8");
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.connect();
            try (OutputStream outputStream = con.getOutputStream()) {
                try (PrintStream ps = new PrintStream(outputStream)) {
                    ps.print(jsonText);                
                }
            }
            
            // recv response
            int statusCode = con.getResponseCode();
            StringBuilder sb = new StringBuilder();
            try (InputStream stream = con.getInputStream()) {
                String line;
                BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
            }
            String responseData = sb.toString();
            JsonObject res = JsonTool.parse(responseData);
            if (res != null) {
                JsonObject header = res.getJsonObject("responseHeader");
                if (header != null) {
                    int status = header.getInt("status");
                    if (status == 0) {
                        System.out.println("     +++++ "+ status);
                    }
                    else {
                        System.out.println("     xxx "+ status);
                    }
                }
            }
        }
        catch (Exception ex) {
            Logger.getLogger(Article.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally {
            if (con != null) {
                con.disconnect();
            }
        }
    }
    
}