Java HTTPServer Post参数
我正在使用Java HTTPServer Post参数,java,httpserver,Java,Httpserver,我正在使用com.sun.net.HTTPServer.HTTPServer包提供的HTTPServer编写服务。我需要将一些相当大的数据作为字节流发送到此服务(比如一百万个整数) 我几乎搜索了所有可用的示例,都指向在URL中发送一个小的GET请求。 我需要知道如何以POST请求的形式发送数据。 另外,可以发送的数据是否有任何限制?如果我没有弄错,您希望以GET请求的形式,而不是使用post,向您的HTTPServer发送请求 在HTTP客户端实现中,您可以设置HTTP头和请求方法: HttpU
com.sun.net.HTTPServer.HTTPServer
包提供的HTTPServer
编写服务。我需要将一些相当大的数据作为字节流发送到此服务(比如一百万个整数)
我几乎搜索了所有可用的示例,都指向在URL中发送一个小的GET
请求。
我需要知道如何以POST
请求的形式发送数据。
另外,可以发送的数据是否有任何限制?如果我没有弄错,您希望以GET请求的形式,而不是使用post,向您的
HTTPServer
发送请求
在HTTP客户端实现中,您可以设置HTTP头和请求方法:
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("GET"); //Or POST
} catch (IOException e) {
e.printStacktrace();
}
我建议使用ApacheHttpClient库,而不是上面答案中的持久URL连接(如果您没有使用Spring进行小程序servlet通信) 在这种情况下,您可以构建一个客户端,并通过HTTP将序列化对象(例如序列化为JSON字符串:)作为POST请求发送到您的服务器:
public HttpResponse sendStuff (args) {
HttpPost post = null;
try {
HttpClient client = HttpClientBuilder.create().build();
post = new HttpPost(servletUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(<nameString>, <valueString>));
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(post);
response.getStatusLine().getStatusCode();
return response;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
公共HttpResponse sendStuff(args){
HttpPost=null;
试一试{
HttpClient client=HttpClientBuilder.create().build();
post=新的HttpPost(servletUrl);
List params=new ArrayList();
添加(新的BasicNameValuePair(,);
post.setEntity(新的UrlEncodedFormEntity(params));
HttpResponse response=client.execute(post);
response.getStatusLine().getStatusCode();
返回响应;
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
但是Spring为您节省了大量时间和麻烦,因此我建议您可以通过将字节写入connections输出流来发送POST请求的数据,如下所示
public static String excutePost(String targetURL, String urlParameters)
{
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
使用POST,可以发送的数据量没有限制。您可以找到有关GET-here限制的详细信息No恰恰相反。我希望数据是sen作为POST,也希望数据是相当大的。