Java没有';无法发送HTTP POST请求

Java没有';无法发送HTTP POST请求,java,http,post,Java,Http,Post,我正在实现一些简单的java类,以便使用POST方法发送HTTP请求,并实现另一个java类以接收请求。 当我通过浏览器(Chrome)或应用程序(在本例中我使用了Postman)发出POST请求时,服务器工作正常,但当我使用java发送HTTP请求时,服务器出现问题 我的sendingHTTP类是“Sender.java”,包含以下代码段: String url = "http://localhost:8082/"; URL obj = new URL(url); HttpURLConnect

我正在实现一些简单的java类,以便使用
POST
方法发送
HTTP
请求,并实现另一个java类以接收请求。
当我通过浏览器(Chrome)或应用程序(在本例中我使用了Postman)发出
POST
请求时,服务器工作正常,但当我使用java发送
HTTP
请求时,服务器出现问题

我的sending
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
请求相关的代码片段(如果需要)。


我只想用
Post
方法发送一个明文作为
HTTP
请求的主体。我在这个网站上读过很多网站,甚至有相关的问题。但它仍然不起作用。换句话说,每当我从“Sender.java”创建一个
HTTP
请求时,“Server.java”中都不会出现任何内容。我只是想知道我的代码片段出了什么问题,我应该如何修复它

发送方url中的“localhost”可能未解析为服务器绑定到的同一ip?尝试更改为127.0.0.1或您的实际IP地址。

尝试使用PrintStream

        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();
        java.io.PrintStream printStream = new java.io.PrintStream(os);
        printStream.println("Just Some Text");
        con.getInputStream();//Send request
        os.flush();
        os.close();

我对此进行了测试,它正在工作:

//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()


当您使用浏览器发送POST表单时,它会以中定义的特定格式发送表单,您必须在发出POST请求时在Java上重新创建该格式

使用这种格式,重要的是将
内容类型
标题设置为
应用程序/x-www-form-urlencoded
,并像在带有get请求的url中一样传递正文

URL obj=新URL(URL);
HttpURLConnection con=(HttpURLConnection)obj.openConnection();
//设置基本post请求
con.setRequestMethod(“POST”);
Map form=newhashmap();
//定义字段
表格.put(“用户名”、“根”);
form.put(“密码”,“sjh76HSn!”);//这显然是一个假密码
//修身
细木工sj=新细木工(&);
for(Map.Entry:arguments.entrySet())
sj.add(URLEncoder.encode(entry.getKey(),“UTF-8”)+“=”
+encode(entry.getValue(),“UTF-8”);
byte[]out=sj.toString().getBytes(StandardCharsets.UTF_8);
int长度=out.length;
//准备我们的“con”对象
con.设置固定长度流线型(长度);
con.setRequestProperty(“内容类型”,“应用程序/json;字符集=UTF-8”);
con.connect();
try(OutputStream os=con.getOutputStream()){
o.写(出);
}

Java中会出现什么错误?难道根本没有收到吗?实际发生了什么?您是否使用了诸如Wireshark之类的网络分析工具来查看实际通过套接字发送的内容(如果有的话)?没有错误。问题是什么都没发生!我希望HTTP Post由Sender.java发送并由Server.java读取,就像我的浏览器或邮递员一样。@JoeC让我们检查一下Sender.java中的URL和/或IP是否与邮递员/Chrome中的URL和/或IP完全相同?是的。确切的URL和确切的端口。通过不调用getResponseCode():“int-httpResult=con.getResponseCode();”来命名该问题。我不知道为什么,但当我添加这一行时,它起了作用,这就是答案!解决了我的问题。非常感谢。正确编码帖子正文是非常重要的。这个答案没有编码任何东西。会导致问题。使用Ferrybig的答案。别忘了冲洗=))
//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();
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

// Setting basic post request
con.setRequestMethod("POST");    

Map<String,String> form = new HashMap<>();

// Define the fields
form.put("username", "root");
form.put("password", "sjh76HSn!"); // This is a fake password obviously

// Build the body
StringJoiner sj = new StringJoiner("&");
for(Map.Entry<String,String> entry : arguments.entrySet())
    sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" 
         + URLEncoder.encode(entry.getValue(), "UTF-8"));
byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8);
int length = out.length;

// Prepare our `con` object
con.setFixedLengthStreamingMode(length);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.connect();
try (OutputStream os = con.getOutputStream()) {
    os.write(out);
}