Java tutorial
/* * 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 model.WebRequestPair; import java.io.IOException; import java.io.StringReader; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * * @author loic */ public abstract class AbstractWebRequest { private String scheme; private String host; private String path; private int port; private List<NameValuePair> listParameters; public AbstractWebRequest(String _scheme, String _host, String _path, int _port) { listParameters = new ArrayList<>(); this.scheme = _scheme; this.host = _host; this.path = _path; this.port = _port; } protected URI buildURI() { try { URIBuilder uriB = new URIBuilder(); uriB.setScheme(scheme).setHost(host); if (port > 0) { uriB.setPort(port); } uriB.setPath(path); for (NameValuePair pair : listParameters) { uriB.setParameter(pair.getName(), pair.getValue()); } return uriB.build(); } catch (URISyntaxException ex) { return null; } } public String requestWithGet() { String s = ""; try { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(buildURI()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); if (entity != null) { s = EntityUtils.toString(entity, "UTF-8"); } } finally { response.close(); } } catch (IOException ex) { return null; } return s; } public Document parseResultXML(String toParse) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(toParse)); return builder.parse(is); } catch (ParserConfigurationException | SAXException | IOException ex) { return null; } } public JSONObject parseResultJSON(String toParse) { try { return (JSONObject) new JSONParser().parse(toParse); } catch (ParseException ex) { return null; } } public void addPath(String _path) { path += _path; } public void addParameterGet(String _name, String _value) { listParameters.add(new WebRequestPair(_name, _value)); } public void removeParameterGet(String _name) { listParameters.remove(new WebRequestPair(_name, "")); } public String getScheme() { return scheme; } public void setScheme(String scheme) { this.scheme = scheme; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public List<NameValuePair> getListParameters() { return listParameters; } public void setListParameters(List<NameValuePair> listParameters) { this.listParameters = listParameters; } }