如何发送post请求的时候传输xml文件?
import java.io.*;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
public static String requestPost(String url,String xmlFileName) { HttpClient client = new HttpClient(); PostMethod myPost=new PostMethod(url); String responseString=null; try{
//将文件内容写入变量 BufferedReader bfr=null; StringBuffer sb=new StringBuffer(); bfr=new BufferedReader(new FileReader(new File(xmlFileName))); String line=null; while ((line=bfr.readLine())!=null){ sb.append(line); } myPost.setRequestEntity(new StringRequestEntity(sb.toString(),"text/xml","utf-8")); int statusCode = client.executeMethod(myPost); if(statusCode == HttpStatus.SC_OK) { BufferedInputStream bis = new BufferedInputStream(myPost.getResponseBodyAsStream()); byte[] bytes = new byte[1024]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); int count = 0; while ((count = bis.read(bytes)) != -1) { bos.write(bytes, 0, count); } byte[] strByte = bos.toByteArray(); responseString = new String(strByte, 0, strByte.length, "utf-8"); bos.close(); bis.close(); } }catch (Exception e){ e.printStackTrace(); } myPost.releaseConnection(); return responseString; }