NoHttpResponseException: The server corporbank.dccnet.com failed to respond

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);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值