Java 没有响应的HTTP处理程序
我正在编写一个测试Java 没有响应的HTTP处理程序,java,service,httphandler,httpserver,Java,Service,Httphandler,Httpserver,我正在编写一个测试HTTPHandler,并连接到使用此测试代码创建的服务器 public static void main( String[] args ) throws Exception { String param = URLEncoder.encode( "raghu", Constants.ENCODING_CHARSET ); String uri = "http://127.0.0.1:" + Constants.PORT_NUMBER + "/t
HTTPHandler
,并连接到使用此测试代码创建的服务器
public static void main( String[] args ) throws Exception {
String param = URLEncoder.encode( "raghu", Constants.ENCODING_CHARSET );
String uri = "http://127.0.0.1:" + Constants.PORT_NUMBER + "/test" + "?" + "welcome=" + param;
URL url = new URL( uri );
HttpURLConnection conn = ( HttpURLConnection ) url.openConnection();
conn.setRequestMethod( "GET" );
conn.setRequestProperty( "Accept", "application/xml" );
conn.setDoOutput( true );
conn.setReadTimeout( 5000 );
InputStream responseCode = conn.getInputStream();
System.out.println();
String toString = convertInputStreamToString( responseCode );
System.out.println( "response-->" + toString );
conn.disconnect();
}
private static String convertInputStreamToString( InputStream inputStream ) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for ( int i; ( i = inputStream.read( b ) ) != -1; ) {
out.append( new String( b, 0, i ) );
}
return out.toString();
}
我的处理程序代码是
public class TestServiceAPI extends BaseServiceAPI {
@Override
public void handle( HttpExchange arg0 ) throws IOException {
Map<String, Object> params = getParams( arg0 );
System.out.println( "Inside test service---> " + params.get( "welcome" ) );
}
}
公共类TestServiceAPI扩展了BaseServiceAPI{
@凌驾
公共无效句柄(HttpExchange arg0)引发IOException{
Map params=getParams(arg0);
System.out.println(“内部测试服务-->”+参数获取(“欢迎”);
}
}
那么,必须从处理程序发送一些响应吗?
这里我的代码停在InputStream responseCode=conn.getInputStream()因为那是我知道的唯一开火的方法。请让我知道我是否做得正确,或者是否有其他方式向服务器发送请求
谢谢。我的错。。答案其实很简单
我只是将其添加到我的处理程序代码中:
request.sendResponseHeaders( 200, 0 );
从API来看,您似乎可以使用而不是打开流(测试它!我没有)
仍然-您应该向响应流写入一些内容,以表示请求已完成且没有错误。一个简单的“OK”就可以了