📄 utils.java~20~
字号:
package webpresstest;
import java.io.*;
import java.net.*;
import java.net.ConnectException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.*; //XML解析器接口
import org.w3c.dom.*; //XML的DOM实现
import org.xml.sax.*;
import java.security.*;
import org.apache.log4j.Logger;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* 提供http,xml通讯的接口
* <p>Description: </p>
*/
public class Utils {
public Utils() {
}
public static String PostXml(String receiveUrl,String strXml)throws Exception{
URL url = new URL(receiveUrl);
URLConnection connection = url.openConnection();
connection.setRequestProperty("Content-Type","text/xml");
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStreamWriter out2 = new OutputStreamWriter(connection.getOutputStream(), "8859_1");
out2.write(strXml);
out2.flush();
byte[] buffer=new byte[4096];
InputStream in=connection.getInputStream();
int bytes_read;
String backStr = "";
while ( (bytes_read = in.read(buffer)) != -1) {
backStr = backStr + new String(buffer);
buffer=new byte[4096];
}
in.close();
out2.close();
return backStr.trim();
//return new String(buffer,"GBK").trim();
}
public static Document receiveXml(HttpServletRequest request)throws Exception{
//直接读取文件
BufferedReader br = new BufferedReader(request.getReader());
//读取字符串
//BufferedReader br = new BufferedReader(new StringReader(""));
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder db = fact.newDocumentBuilder();
Document doc = db.parse(new InputSource(br));
System.out.print( br.readLine());
return doc;
}
public static String receiveXml2Str(HttpServletRequest request)throws Exception{
String line=null;
String returnxml="";
//直接读取文件
BufferedReader br = new BufferedReader(request.getReader());
while ((line = br.readLine()) != null){
returnxml=returnxml+line;
}
return returnxml;
}
public static String getUrl(String UrlStr) throws Exception {
/*System.setProperty("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
*/
Logger Loger2 = Logger.getLogger("ERROR");
InputStream in=null;
OutputStream out=null;
byte[] buffer=new byte[4096];
try{
URL url=new URL(UrlStr);
in=url.openStream();
int bytes_read;
String backStr="";
while((bytes_read=in.read(buffer))!=-1){
backStr=backStr+new String(buffer);
buffer=new byte[4096];
}
Loger2.error(backStr.trim());
return backStr.trim();
//return new String(buffer,"GBK").trim();
}
catch (ConnectException e){
System.out.println("ConnectException");
System.out.println(e);
throw e;
//throw new HttpClientException("error.connection");
}
catch (IOException e){
System.out.println("IOException");
System.out.println(e);
throw e;
//throw new HttpClientException("error.io");
}
finally{
try{in.close();}
catch (Exception e){
//e.printStackTrace();
// System.out.println("error");
}
try{out.close();}
catch (Exception e){
//e.printStackTrace();
// System.out.println("error");
}
}
}
public static String replace(String strSource, String strFrom, String strTo) {
if(strSource==null || strFrom==null || strTo==null){
return strSource;
}else{
java.lang.String strDest = "";
int intFromLen = strFrom.length();
int intPos;
while ( (intPos = strSource.indexOf(strFrom)) != -1) {
strDest = strDest + strSource.substring(0, intPos);
strDest = strDest + strTo;
strSource = strSource.substring(intPos + intFromLen);
}
strDest = strDest + strSource;
return strDest;
}
}
public static String http_get(String _url)throws Exception{
HttpClient client = new HttpClient();
String response ="";
GetMethod method = new GetMethod(_url);
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
response = method.getResponseBodyAsString();
}
return response.trim();
}
public static String http_post(String _url,String _content)throws Exception{
int BASE_BODY_SIZE = 10240;
int INC_BODY_SIZE = 51200;
String result ="";
DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
retryhandler.setRequestSentRetryEnabled(true);
retryhandler.setRetryCount(2); // retry 2 times
HttpClient httpClient = new HttpClient();
PostMethod method = new PostMethod(_url);
InputStream data = new ByteArrayInputStream(_content.getBytes());
method.setRequestBody(data);
System.out.println("mmm"+_content);
//method.setFollowRedirects(true);
method.setMethodRetryHandler(retryhandler);
/*
int retcode = httpClient.executeMethod(method);
if (retcode == HttpStatus.SC_OK) {
byte[] responseBody = new byte[BASE_BODY_SIZE];
java.io.InputStream istream = method.getResponseBodyAsStream();
int npos = 0;
int nread = 0;
while ( (nread = istream.read(responseBody, npos,
responseBody.length - npos)) >= 0) {
npos += nread;
if (npos >= responseBody.length) {
byte[] tmpBuf = new byte[npos + INC_BODY_SIZE];
System.arraycopy(responseBody, 0, tmpBuf, 0, npos);
responseBody = tmpBuf;
}
}
result = new String(responseBody, 0, npos);
}
return result.trim();*/
if (method.getStatusCode() == HttpStatus.SC_OK) {
result = method.getResponseBodyAsString();
}
return result.trim();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -