import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Date; import java.util.HashMap; import java.util.List; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; public class Test { /** * @param args */ public static void main(String[] args) throws Exception{ //直接字符串拼接 StringBuffer sb = new StringBuffer(); sb.append("<app_ei_sync_req><enabler_id>pengxwtest</enabler_id><dev_id>pengxwtest</dev_id>" + "<app_id>pengxwtest</app_id><app_secret>pengxwtest</app_secret>" + "<app_status>2</app_status><app_level>0</app_level><app_ei><ei_id>1</ei_id>" + "<ei_id>2</ei_id><ei_id>3</ei_id></app_ei></app_ei_sync_req>");//xml数据存储 String data = sb.toString(); String url = "接口地址"; HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod(url); String info = null; try { RequestEntity entity = new StringRequestEntity(data, "text/xml", "iso-8859-1"); post.setRequestEntity(entity); httpclient.executeMethod(post); int code = post.getStatusCode(); if (code == HttpStatus.SC_OK) info = new String(post.getResponseBodyAsString()); //接口返回的信息 } catch (Exception ex) { ex.printStackTrace(); } finally { post.releaseConnection(); } System.out.println(info); } //读取xml文件 public class xmlTool(){ InputStreamReader read = new InputStreamReader (new FileInputStream("f://aa.xml"),"UTF-8"); StringBuffer sb = new StringBuffer(); BufferedReader br = new BufferedReader(read); String row; while((row = br.readLine())!=null){ sb.append(row.trim()); } String data = sb.toString(); String url = "http://localhost:9099/vtoss/cloudapi/rp_video_transcode_batch.do"; HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod(url); String info = null; try { RequestEntity entity = new StringRequestEntity(data, "text/xml", "UTF-8"); post.setRequestEntity(entity); httpclient.executeMethod(post); int code = post.getStatusCode(); if (code == HttpStatus.SC_OK) info = new String(post.getResponseBodyAsString()); } catch (Exception ex) { ex.printStackTrace(); } finally { post.releaseConnection(); } System.out.println(info); } }
转向的处理
private void postMethod(String url) throws IOException { url = "http://www.newsmth.net/bbslogin2.php"; PostMethod postMethod = new PostMethod(url); // 填入各个表单域的值 NameValuePair[] data = { new NameValuePair("id", "herrapfel"),new NameValuePair("passwd", "") }; // 将表单的值放入postMethod中 postMethod.setRequestBody(data); // 执行postMethod int statusCode = httpClient.executeMethod(postMethod); System.out.println(" status code:" + statusCode); // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发 if(statusCode == HttpStatus.SC_OK) { StringBuffer contentBuffer = new StringBuffer(); InputStream in = postMethod.getResponseBodyAsStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in,postMethod.getResponseCharSet())); String inputLine = null; while((inputLine = reader.readLine()) != null) { contentBuffer.append(inputLine); System.out.println("input line:"+ inputLine); contentBuffer.append("/n"); } in.close(); } else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { // 从头中取出转向的地址 Header locationHeader = postMethod.getResponseHeader("location"); String location = null; if (locationHeader != null) { location = locationHeader.getValue(); System.out.println("The page was redirected to:" + location); } else { System.err.println("Location field value is null."); } } }
从文件读取
import java.io.File; import java.io.FileInputStream; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.EntityEnclosingMethod; import org.apache.commons.httpclient.methods.PostMethod; /** * 用来演示提交XML格式数据的例子 */ public class PostXMLClient { public static void main(String[] args) throws Exception { File input = new File(“test.xml”); PostMethod post = new PostMethod(“http://localhost:8080/httpclient/xml.jsp”); // 设置请求的内容直接从文件中读取 post.setRequestBody(new FileInputStream(input)); if (input.length() < Integer.MAX_VALUE) post.setRequestContentLength(input.length()); else post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); // 指定请求内容的类型 post.setRequestHeader("Content-type", "text/xml; charset=GBK"); HttpClient httpclient = new HttpClient(); int result = httpclient.executeMethod(post); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); post.releaseConnection(); } }
最新评论