码迷,mamicode.com
首页 > 编程语言 > 详细

java后台调用url

时间:2014-06-21 23:35:20      阅读:426      评论:0      收藏:0      [点我收藏+]

标签:des   class   blog   code   java   http   

         QXOutStream  outPut= new QXOutStream();
         qxWorkSheetXML.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	qxWorkSheetXML.append("<ROOT><ROW>");
	qxWorkSheetXML.append("<REPORT_MAN>"+call_man+"</REPORT_MAN>");
	qxWorkSheetXML.append("<ACCEPT_TIME>"+call_time+"</ACCEPT_TIME>");
	qxWorkSheetXML.append("<CUSTOMER_CODE>"+workSheetNo+"</CUSTOMER_CODE>");
	qxWorkSheetXML.append("<PHONE>"+call_no+"</PHONE>");
	qxWorkSheetXML.append("<DEAL_DATE>"+deadLine+"</DEAL_DATE>");
	qxWorkSheetXML.append("</ROW></ROOT>");

	returnStr=outPut.outPutStr(urlStr, qxWorkSheetXML.toString());


 

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import XmlHelper;

public class QXOutStream {
	public String outPutStr(String urlStr, String input) throws Exception{
		StringBuffer strBuf = new StringBuffer();
		String Resulst="";
		try{
			URL url = new URL(urlStr);
			HttpURLConnection con = (HttpURLConnection)url.openConnection();
			con.setDoInput(true);
			con.setDoOutput(true);
			con.setRequestMethod("POST");
			con.setAllowUserInteraction(false);			
			con.setUseCaches(false);
			con.setRequestProperty("Accept-Charset", "GBK");
			BufferedOutputStream bufOutPut = new BufferedOutputStream(con.getOutputStream());
			
			byte[] bdat = input.getBytes("UTF-8");//解决中文乱码问题
	        bufOutPut.write(bdat, 0, bdat.length);
			bufOutPut.flush();
			BufferedInputStream inp = new BufferedInputStream(con.getInputStream());
			InputStreamReader in = new InputStreamReader(inp,Charset.forName("GBK"));
			BufferedReader bufReador = new BufferedReader(in);

			String tempStr = "";
			while (tempStr != null) {
				strBuf.append(tempStr);
				tempStr = bufReador.readLine();
				
			}
			Resulst = XmlHelper.getPostNodeText(strBuf.toString(), "OPERATOR_RESULT");//.getPostFirstRowText(strBuf.toString(), "OPERATOR_RESULT");
			
		}
		catch (Exception e) {
			//System.err.println("Exception:"+e.toString());
			throw e;
			//return "N";
		}
		finally{
			return Resulst;
		}

	}
}


 

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.transform.TransformerException;

import org.apache.log4j.Logger;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sun.org.apache.xpath.internal.XPathAPI;

/**
 * 处理XML的封装类
  */
public abstract class XmlHelper {
	private XmlHelper() {
	}

	private static SimpleXmlParser parser = new SimpleXmlParser();

	private static Logger logger = Logger.getLogger(XmlHelper.class.getName());

	public static Document getDocument(String xmlStr) throws Exception {
		try {
			return parser.parseXml(xmlStr);
		} catch (Exception ex) {
			logger.error("得到Document出错-->", ex);
			throw ex;
		}
	}

	public static Document getDocumentFormStream(InputStream s)
			throws Exception {
		try {
			Document doc = parser.parse(s);
			return doc;
		} catch (Exception ex) {
			logger.error("从文件流中得到Document出现错误,错误为-->", ex);
			throw ex;
		}
	}

	public static Document getDocumentFormFile(String url) {
		try {
			URL u = new URL(url);
			URLConnection connection = u.openConnection();
			connection.setDoInput(true);
			connection.setUseCaches(false);
			Document doc = parser.parse(url);
			connection.getInputStream().close();
			return doc;
		} catch (Exception ex) {
			logger.info("从url中取得数据出错,错误为-->", ex);
		}
		return null;
	}

	public static NodeList selectNodeList(Node node, String xpath)
			throws TransformerException {
		try {
			return XPathAPI.selectNodeList(node, xpath);
		} catch (TransformerException ex) {
			logger.error("得到xml节点队列出错-->", ex);
			throw ex;
		}
	}

	public static Node selectSingleNode(Node node, String xpath)
			throws TransformerException {
		try {
			return XPathAPI.selectSingleNode(node, xpath);
		} catch (TransformerException ex) {
			logger.error("得到单一的xml节点出错-->", ex);
			throw ex;
		}
	}

	public static Node selectNode(Node node, String xpath) {
		for (int i = 0; i < node.getChildNodes().getLength(); i++) {
			Node childNode = node.getChildNodes().item(i);
			if (childNode.getNodeType() == Node.ELEMENT_NODE
					&& childNode.getNodeName().equals(xpath))
				return childNode;
		}
		return null;
	}

	public static Attr getAttribute(Node node, String attName) {
		return (Attr) node.getAttributes().getNamedItem(attName);
	}

	public static String getNodeText(Node node) {
		for (int i = 0; i < node.getChildNodes().getLength(); i++) {
			if (node.getChildNodes().item(i).getNodeType() == Node.TEXT_NODE)
				return node.getChildNodes().item(i).getNodeValue();
		}
		return null;
	}

	public static String getPostNodeText(String postString, String nodeName) {
		try {
			Document doc = getDocument(postString);
			Element root = doc.getDocumentElement();
			NodeList list = root.getChildNodes();

			for (int i = 0; i < list.getLength(); i++) {
				Node node = list.item(i);
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					logger.debug("node:" + node.getNodeType() + ","
							+ node.getNodeName());
					if (node.getNodeName().equals(nodeName))
						return getNodeText(node);
				}
			}
		} catch (Exception ex) {
			logger.error("从post信息中得到xml解析数据出错-->", ex);
		}
		return null;
	}

	public static String getPostFirstRowText(String postString, String nodeName) {
		try {
			return XmlHelperJdom.getFirstRowData(postString, nodeName);
		} catch (Exception ex) {
			logger.error("从post的第一行数据中得到xml解析数据出错-->", ex);
		}
		return null;
	}

	/*
	 * 添加一个节点,并且设置其text
	 */
	public static org.dom4j.Element addElementAndSetValue(
			org.dom4j.Element parent, String name, String value) {
		if (null == name)
			return null;
		org.dom4j.Element e = parent.addElement(name);
		if (null == value) {
			value = "";
		}
		e.setText(value);
		return e;
	}
}


 

java后台调用url,布布扣,bubuko.com

java后台调用url

标签:des   class   blog   code   java   http   

原文地址:http://blog.csdn.net/myfmyfmyfmyf/article/details/32690757

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!