ception in thread "main" org.apache.commons.httpclient.NoHttpResponseException: The server corporbank.dccnet.com failed to respond
服务器没有响应。
使用jdk httpserver 一些业务逻辑,导致异常,没有返回给客户端数据,导致异常。
package com.cyyun.xc.command.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.spi.HttpServerProvider;
@SuppressWarnings("all")
public abstract class ScreenshotHandler {
private static Logger log = Logger.getLogger(ScreenshotHandler.class);
private int port = 6666;
private String path = "test";
public ScreenshotHandler() {
}
public ScreenshotHandler(int port, String path) {
this.port = port;
this.path = path;
}
// 启动服务,监听来自客户端的请求
public void httpserverService() throws IOException {
HttpServerProvider provider = HttpServerProvider.provider();
HttpServer httpserver = provider.createHttpServer(
new InetSocketAddress(port), 100);// 监听端口6666,能同时接 受100个请求
httpserver.createContext(path, new MyHttpHandler());
httpserver.setExecutor(null);
httpserver.start();
// System.out.println("server started");
log.info("server started");
}
// Http请求处理类
public class MyHttpHandler implements HttpHandler {
public void handle(HttpExchange httpExchange) throws IOException {
// String responseMsg = "{result:success}"; // 响应信息
log.error("MyHttpHandler。。。。。。。。。。。。。。。。。。。。。。。");
String responseMsg = "";
InputStream in = null;
BufferedReader reader = null;
OutputStream out = null;
try {
in = httpExchange.getRequestBody(); // 获得输入流
reader = new BufferedReader(new InputStreamReader(in));
String temp = null;
StringBuilder params = new StringBuilder();
while ((temp = reader.readLine()) != null) {
log.info("client request:" + temp);
params.append(temp);
}
Map<String, String> map = convertMap(params.toString());
responseMsg = callback(map);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
httpExchange.sendResponseHeaders(200, responseMsg.length()); // 设置响应头属性及响应信息的长度
out = httpExchange.getResponseBody(); // 获得输出流
out.write(responseMsg.getBytes());
out.flush();
if (in != null) {
in.close();
}
if (reader != null) {
reader.close();
}
if (out != null) {
out.close();
}
httpExchange.close();
}
}
}
/**
* 回调方法,返回结果"{result:success}"
*
* @param map
* @return
*/
public abstract String callback(Map<String, String> map);
public static Map<String, String> convertMap(String str) {
Map<String, String> map = new HashMap<String, String>();
if (StringUtils.isEmpty(str)) {
return map;
}
String[] names = str.split("\\&");
if (ArrayUtils.isEmpty(names)) {
return map;
}
try {
for (String name : names) {
if (StringUtils.isNotEmpty(name)) {
String[] vals = name.split("\\=");
if (ArrayUtils.isNotEmpty(vals)) {
if (vals.length == 1) {
map.put(vals[0], null);
} else {
map.put(vals[0], vals[1]);
}
}
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return map;
}
}
public static String getConfigValue(String key)
{
Locale locale = Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("configParameter", locale);
return localResource.getString(key);
}