Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This method works for every request. "POST" is given as String.</p> <pre><code> * Sends generic HTTP calls: GET, POST, DELETE etc provided the parameters. Also includes the * response body in the returned object. */ private WebCallResponse sendRequest(String url, WebCallHeader[] headers, String method, byte[] postBytes) throws WebServiceException { HttpURLConnection conn = null; URL serverUrl = null; OutputStream os = null; InputStream is = null; try { serverUrl = new URL(url); conn = (HttpURLConnection)serverUrl.openConnection(); conn.setRequestMethod(method); conn.setRequestProperty("Accept-Encoding", "deflated"); conn.setRequestProperty("Accept-Ranges", "bytes"); if(headers != null &amp;&amp; headers.length &gt; 0) { for(int i = 0; i &lt; headers.length; ++i) { conn.setRequestProperty(headers[i].getHeaderName(), headers[i].getHeaderValue()); } } //-- For POSTs and PUTs if(postBytes != null &amp;&amp; postBytes.length &gt; 0) { conn.setDoOutput(true); conn.setRequestProperty("Content-Length", String.valueOf(postBytes.length)); conn.connect(); os = conn.getOutputStream(); os.write(postBytes); } //-- Execute happens here. final int rc = conn.getResponseCode(); //-- Build return object from the response. final WebCallResponse wcr = new WebCallResponse(); wcr.setStatusCode(rc); if (conn.getHeaderField("record-count") != null) { final Vector headerVector = new Vector(); headerVector.add(conn.getHeaderField("record-count")); wcr.setHeaders(headerVector); } is = new BufferedInputStream(conn.getInputStream()); wcr.setResponseString(this.getResponseBody(is)); return wcr; } catch(IOException ioe) { conn.getErrorStream(); throw new WebServiceException(ioe, "Failed to open connection to host."); }finally { try{if(is != null)is.close();}catch(Exception squish){} try{if(os != null)os.close();}catch(Exception squish){} try{if(conn != null)conn.disconnect();}catch(Exception squish){} } } public class WebCallResponse { private int statusCode; private String responseString; private Vector headers; public Vector getHeaders() { return headers; } public void setHeaders(Vector headers) { this.headers = headers; } public void setStatusCode(int statusCode) { this.statusCode = statusCode; } public int getStatusCode() { return statusCode; } public void setResponseString(String responseString) { this.responseString = responseString; } public String getResponseString() { return responseString; } public boolean isRequestSuccessful(){ return (statusCode &gt;=200 &amp;&amp; statusCode &lt;300); } public String getHeaderValue(String key) { WebCallHeader currentHeader; String result = null; for (int i = 0; i &lt; this.headers.size(); i++) { currentHeader = ((WebCallHeader) this.headers.elementAt(i)); if (currentHeader.getHeaderName().equalsIgnoreCase(key)) { result = currentHeader.getHeaderValue(); } } return result; } public String toString() { return this.responseString; } </code></pre> <p>}</p> <pre><code>public class WebCallHeader { private String headerName; private String headerValue; public WebCallHeader(String name,String value) { this.headerName = name; this.headerValue = value; } public void setHeaderName(String headerName) { this.headerName = headerName; } public String getHeaderName() { return headerName; } public void setHeaderValue(String headerValue) { this.headerValue = headerValue; } public String getHeaderValue() { return headerValue; } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload