/**
* 发送消息
* @param text
* @throws IOException
* @throws UnsupportedEncodingException
*/
public void send(String text) throws UnsupportedEncodingException, IOException
{
long startTime=System.currentTimeMillis();
HttpURLConnection urlConnection = null;
OutputStreamWriter osw=null;
BufferedReader reader=null;
String response =null;
try
{
// 发送请求
URL url = new URL(this.requestUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
// 设置超时时间
urlConnection.setConnectTimeout(5000);
// urlConnection.setReadTimeout(10000);
// POST请求
urlConnection.setRequestMethod(Constants.REQUEST_POST);
// 不适用缓存
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "text/xml; charset=" + Constants.ENCODING_UTF8);
urlConnection.setRequestProperty("Connection", "close");
osw = new OutputStreamWriter(urlConnection.getOutputStream(), Constants.ENCODING_UTF8);
osw.write(text + "\r\n");
osw.flush();
// log.debug("Send Request info:" + text);
// 读取请求返回值
StringBuffer sb = new StringBuffer();
InputStream errorStream = urlConnection.getErrorStream();
if (errorStream != null)
{
BufferedReader error = new BufferedReader(new InputStreamReader(errorStream, Constants.ENCODING_UTF8));
String line;
while ((line = error.readLine()) != null)
{
sb.append(new String(line.getBytes(Constants.ENCODING_UTF8), Constants.ENCODING_UTF8));
}
errorStream.close();
error.close();
}
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),
Constants.ENCODING_UTF8));
String line;
while ((line = reader.readLine()) != null)
{
sb.append(new String(line.getBytes(Constants.ENCODING_UTF8), Constants.ENCODING_UTF8));
}
response = sb.toString();
// log.debug("Response info:" + response);
}
finally
{
if(osw!=null){
osw.close();
}
if(reader!=null){
reader.close();
}
if(urlConnection!=null)
{
urlConnection.disconnect();
}
StringBuffer sb=new StringBuffer();
sb.append("request xml =").append(text).append("\r\nresponse=").append(response);
sb.append(",task time=").append((System.currentTimeMillis()-startTime)).append("ms");
log.info(sb);
}
}
* 发送消息
* @param text
* @throws IOException
* @throws UnsupportedEncodingException
*/
public void send(String text) throws UnsupportedEncodingException, IOException
{
long startTime=System.currentTimeMillis();
HttpURLConnection urlConnection = null;
OutputStreamWriter osw=null;
BufferedReader reader=null;
String response =null;
try
{
// 发送请求
URL url = new URL(this.requestUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
// 设置超时时间
urlConnection.setConnectTimeout(5000);
// urlConnection.setReadTimeout(10000);
// POST请求
urlConnection.setRequestMethod(Constants.REQUEST_POST);
// 不适用缓存
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "text/xml; charset=" + Constants.ENCODING_UTF8);
urlConnection.setRequestProperty("Connection", "close");
osw = new OutputStreamWriter(urlConnection.getOutputStream(), Constants.ENCODING_UTF8);
osw.write(text + "\r\n");
osw.flush();
// log.debug("Send Request info:" + text);
// 读取请求返回值
StringBuffer sb = new StringBuffer();
InputStream errorStream = urlConnection.getErrorStream();
if (errorStream != null)
{
BufferedReader error = new BufferedReader(new InputStreamReader(errorStream, Constants.ENCODING_UTF8));
String line;
while ((line = error.readLine()) != null)
{
sb.append(new String(line.getBytes(Constants.ENCODING_UTF8), Constants.ENCODING_UTF8));
}
errorStream.close();
error.close();
}
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),
Constants.ENCODING_UTF8));
String line;
while ((line = reader.readLine()) != null)
{
sb.append(new String(line.getBytes(Constants.ENCODING_UTF8), Constants.ENCODING_UTF8));
}
response = sb.toString();
// log.debug("Response info:" + response);
}
finally
{
if(osw!=null){
osw.close();
}
if(reader!=null){
reader.close();
}
if(urlConnection!=null)
{
urlConnection.disconnect();
}
StringBuffer sb=new StringBuffer();
sb.append("request xml =").append(text).append("\r\nresponse=").append(response);
sb.append(",task time=").append((System.currentTimeMillis()-startTime)).append("ms");
log.info(sb);
}
}