Java tutorial
/* * Copyright (C) 2014 Artitelly Solutions Inc, www.CloudTestSoftware.com * * Licensed under the Common Development and Distribution License (CDDL-1.0) (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://opensource.org/licenses/CDDL-1.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.testmax.util; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import com.testmax.framework.WmLog; public class HttpUtil { public HttpUtil() { // TODO Auto-generated constructor stub } /* * @param = parameter to post * @xml= xml data to post * @url= url to post */ public String postRequest(String url, String param, String xml) { String resp = ""; PostMethod post = new PostMethod(url); if (param.equalsIgnoreCase("body")) { //xmlData=urlConf.getUrlParamValue(param); byte buf[] = xml.getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(buf); post.setRequestEntity(new InputStreamRequestEntity(new BufferedInputStream(in), xml.length())); } else { post.setRequestHeader(param, xml); } post.setRequestHeader("Content-type", "text/xml; charset=utf-8"); // Get HTTP client org.apache.commons.httpclient.HttpClient httpsclient = new org.apache.commons.httpclient.HttpClient(); // Execute request try { int response = -1; try { response = httpsclient.executeMethod(post); resp = post.getResponseBodyAsString(); Header[] heads = post.getResponseHeaders(); String header = ""; for (Header head : heads) { header += head.getName() + ":" + head.getValue(); } System.out.println("Header=" + header); WmLog.printMessage("Response: " + resp); WmLog.printMessage("Response Header: " + header); } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } finally { // Release current connection to the connection pool // once you are done post.releaseConnection(); } return resp; } public int postFormdata(String myurl, String param, String xml) { HttpURLConnection httpConn = null; URL url; int rest = -1; try { url = new URL(myurl); httpConn = (HttpURLConnection) url.openConnection(); httpConn.setRequestProperty("content-type", "text/xml; charset=utf-8"); httpConn.setDoOutput(true); OutputStream os = httpConn.getOutputStream(); BufferedWriter osw = new BufferedWriter(new OutputStreamWriter(os)); osw.write(xml); osw.flush(); osw.close(); rest = httpConn.getResponseCode(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rest; } public String getHTML(String urlToRead) { URL url; HttpURLConnection conn; BufferedReader rd; String line; String result = ""; try { url = new URL(urlToRead); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = rd.readLine()) != null) { result += line; } rd.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return result; } }