package com.test;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringBufferInputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class TestStateQuery {
public String connectServlet(String url,String businessnoXml){
String strReturnXML="";
HttpURLConnection httpConnection;
try {
// 1、打开连接
httpConnection = (HttpURLConnection) new URL(url).openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setAllowUserInteraction(true);
httpConnection.connect();
// 2、发送数据
OutputStream outputStream = httpConnection.getOutputStream();
byte[] buffer = new byte[1024];
// InputStream stringBufferInputStream = new StringBufferInputStream(businessnoXml);
InputStream stringBufferInputStream = new ByteArrayInputStream(businessnoXml.getBytes());
while (true) {
int bytesRead = stringBufferInputStream.read(buffer);
if (bytesRead == -1)
break;
outputStream.write(buffer, 0, bytesRead);
//outputStream.write(buffer);
}
outputStream.flush();
stringBufferInputStream.close();
outputStream.close();
// 3、返回数据
InputStreamReader inputStreamReader = new InputStreamReader(httpConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String inputLine = "";
StringBuffer inputLines = new StringBuffer();
while ((inputLine = bufferedReader.readLine()) != null) {
inputLines.append(inputLine);
}
inputStreamReader.close();
bufferedReader.close();
// 4、关闭连接
httpConnection.disconnect();
strReturnXML = inputLines.toString();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strReturnXML.toString();
}
public static void main(String[] args) {
String resultXML = "";
TestStateQuery test = new TestStateQuery();
String url = "http://localhost:7001/undwrt/ServletName";
String filePath ="E:/sendXML/zhongxin.xml";
resultXML = test.connectServletUserXmlFile(url, filePath);
//resultXML = test.connectServlet( url, businessno);
System.out.println("resultXML:"+resultXML);
}
public String connectServletUserXmlFile(String url,String filePath){
String strReturnXML="";
HttpURLConnection httpConnection;
try {
// 1、打开连接
httpConnection = (HttpURLConnection) new URL(url).openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setAllowUserInteraction(true);
httpConnection.connect();
// 2、发送数据
OutputStream outputStream = httpConnection.getOutputStream();
byte[] buffer = new byte[1024];
// InputStream stringBufferInputStream = new StringBufferInputStream(businessnoXml);
//InputStream stringBufferInputStream = new ByteArrayInputStream(businessnoXml.getBytes());
File file = new File(filePath);
InputStream in = new FileInputStream(file);
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1)
break;
outputStream.write(buffer, 0, bytesRead);
//outputStream.write(buffer);
}
outputStream.flush();
in.close();
outputStream.close();
// 3、返回数据
InputStreamReader inputStreamReader = new InputStreamReader(httpConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String inputLine = "";
StringBuffer inputLines = new StringBuffer();
while ((inputLine = bufferedReader.readLine()) != null) {
inputLines.append(inputLine);
}
inputStreamReader.close();
bufferedReader.close();
// 4、关闭连接
httpConnection.disconnect();
strReturnXML = inputLines.toString();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strReturnXML.toString();
}
}
java 远程访问接口
最新推荐文章于 2022-03-11 23:08:07 发布