Java不发送HTTP POST请求 [英] Java doesn't send HTTP POST Request

查看:174
本文介绍了Java不发送HTTP POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一些简单的Java类,以便通过POST方法发送HTTP请求,并且还实现了另一个Java类,以便接收该请求.
当我通过浏览器(Chrome)或应用程序(在这种情况下使用Postman)发出POST请求时,服务器工作正常,但是当我用java发送HTTP请求时,服务器最终出现问题! /p>

我发送的HTTP类是"Sender.java",其中包含以下代码段:

String url = "http://localhost:8082/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

// Setting basic post request
con.setRequestMethod("POST");       
//con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
//con.setRequestProperty("Content-Type","text/plain");

// Send post request
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write("Just Some Text".getBytes("UTF-8"));
os.flush();
os.close();
//connect to the Server(resides at Server.java)
con.connect();

我已经注释了几行代码设置标头,例如"Accept-Language"和"Content-Type",因为我不知道Java程序是否需要这些标头?

服务器是另一个名为"Server.java"的Java程序.这是与读取Sender.java发出的HTTP请求有关的代码段.(如果需要).

int servPort = 8082;
// Create a server socket to accept HTTP client connection requests
HttpServer server = HttpServer.create(new InetSocketAddress(servPort), 0);
System.out.println("server started at " + servPort);

server.createContext("/", new PostHandler());//PostHandler implements HttpHandler
server.setExecutor(null);

server.start();

我想要的只是用Post方法发送纯文本作为我的HTTP请求的正文.我已经阅读了很多网站,甚至在这个网站上有相关的问题.但它仍然无法解决.换句话说,每当我从"Sender.java"创建一个HTTP请求时,"Server.java"都不会出现任何内容.我只想知道摘要的问题以及如何解决?

解决方案

我对此进行了测试,并且可以正常工作:

//Sender.java
String url = "http://localhost:8082/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();


con.setRequestMethod("POST");       
con.setDoOutput(true);

OutputStream os = con.getOutputStream();
os.write("Just Some Text".getBytes("UTF-8"));
os.flush();

int httpResult = con.getResponseCode(); 
con.disconnect();

如您所见,连接不是必需的.关键行是

int httpResult = con.getResponseCode();

I'm implementing some simple java class in order to send an HTTP Request with POST method and also another java class in order to receive it.
The server works fine when I make a POST request by means of my browser(Chrome), or an application(I have used Postman in this case) but it ends up with problem when I send HTTP Request with java!

My sending HTTP class is "Sender.java", containing the following snippet:

String url = "http://localhost:8082/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

// Setting basic post request
con.setRequestMethod("POST");       
//con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
//con.setRequestProperty("Content-Type","text/plain");

// Send post request
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write("Just Some Text".getBytes("UTF-8"));
os.flush();
os.close();
//connect to the Server(resides at Server.java)
con.connect();

I have commented some lines of code setting Headers like "Accept-Language" and "Content-Type" because I don't know whether or not are these headers required for the java program to work out?

The server is another java program named "Server.java". Here is the snippet related to reading HTTP Request made by the Sender.java(if need be).

int servPort = 8082;
// Create a server socket to accept HTTP client connection requests
HttpServer server = HttpServer.create(new InetSocketAddress(servPort), 0);
System.out.println("server started at " + servPort);

server.createContext("/", new PostHandler());//PostHandler implements HttpHandler
server.setExecutor(null);

server.start();

All I want is to send a plaintext as the body of my HTTP Request with the Post method. I have read plenty of sites and even related questions at this site. But it still doesn't work out. In other words, whenever I create an HTTP Request from "Sender.java", nothing appears at "Server.java". I just want to know what's wrong with my snippets and how should I fix that?

解决方案

I tested this and it's working:

//Sender.java
String url = "http://localhost:8082/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();


con.setRequestMethod("POST");       
con.setDoOutput(true);

OutputStream os = con.getOutputStream();
os.write("Just Some Text".getBytes("UTF-8"));
os.flush();

int httpResult = con.getResponseCode(); 
con.disconnect();

As you can see, connect is not necessary. The key line is

int httpResult = con.getResponseCode();

这篇关于Java不发送HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆