HttpClient发送消息
来源:互联网 发布:ps4生化危机7淘宝禁售 编辑:程序博客网 时间:2024/06/15 18:43
/**
* 发送消息
* @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);
}
}
0 0
- HttpClient发送消息
- httpclient 发送消息体
- jsdt 插件 httpclient模型发送消息至服务端
- cocos2d-x httpclient发送get或者post消息请求
- Python学习笔记(九) -- Python HttpClient使用requests库发送http消息
- HttpClient远程发送文件
- HttpClient发送URL
- Httpclient发送POST请求
- HttpClient发送请求
- httpClient发送post请求
- httpCLient 发送请求
- HttpClient发送url请求
- HttpClient发送Request请求
- httpclient发送put请求
- HTTPClient 发送HTTPS请求
- httpClient 发送 http请求
- HttpClient 4.3 发送HTTPS
- httpClient发送请求
- Jetbrains
- 回调函数 学习
- Mongodb在Ubuntu下的安装配置(单机)
- CDH5.3.2安装详细文档以及相关问题处理
- stat函数讲解
- HttpClient发送消息
- iOS6 ios7 图片适配
- android使用ListView和Adapter.getView Recycler的工作原理
- 谈谈自己对随机森林(Random Forest)的一点理解以及代码注释~
- 程序员和计算机语言的发展
- 【解决方案】Sublime Text 2 打开lua为dump格式
- Linux内核初探5
- Sqlite插入或更新
- STM32F107 USB otg U盘读写