【发布时间】:2014-04-22 13:48:03
【问题描述】:
我正在编写一个测试 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" ) );
}
}
那么必须从处理程序发送一些响应吗?
我的代码在这里停在InputStream responseCode = conn.getInputStream();,因为这是我知道的唯一触发方式。请让我知道是否正确执行或是否有任何其他方式向服务器发出请求。
谢谢。
【问题讨论】:
标签: java service httphandler httpserver