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

4795 단어 exception
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);
    }

좋은 웹페이지 즐겨찾기