post 方式提交XML文件调用接口
生活随笔
收集整理的這篇文章主要介紹了
post 方式提交XML文件调用接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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();
}
}
總結
以上是生活随笔為你收集整理的post 方式提交XML文件调用接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php excel 设置单元格格式为文本
- 下一篇: UVA 3485 Bridge