gpt4 book ai didi

java - 解析 RESTful PUT

转载 作者:行者123 更新时间:2023-11-30 03:43:49 24 4
gpt4 key购买 nike

如何在 Java 服务器应用程序中访问 HTTP PUT 消息(JSON 格式)? HTTP PUT 是从 Python 客户端发送的。

到目前为止的 Python 客户端:

    import http.client 
import urllib

values = {'s':'basic','submit':'search'}
data = urllib.parse.urlencode(values)


headers = headers['Content-length']=str(len(bytes(data, 'utf-8')))
connection = http.client.HTTPConnection('localhost',8080)
connection.request("PUT", "/file", body=data.encode("utf-8"))

当 PUT 到达 Java 服务器时 - 如何获取消息(来自 Python 的数据)?

到目前为止,这就是我在 Java 中所拥有的:

import java.io.*;
import java.net.*;

class JavaServer {

public static void main(String args[]) throws Exception {

String fromclient;

ServerSocket Server = new ServerSocket (8080);

System.out.println("TCPServer Waiting for client on port 8080");

Socket connected = Server.accept();
System.out.println( " THE CLIENT"+" "+ connected.getInetAddress() +":"+connected.getPort()+" IS CONNECTED ");

BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connected.getInputStream()));

fromclient = inFromClient.readLine();
System.out.println( "RECIEVED:" + fromclient );

}
}

最佳答案

您需要迭代“inFromClient”

所以类似:

    while((fromclient = inFromClient.readLine()) != null){
System.out.println( "RECIEVED:" + fromclient );
}
inFromClient.close();

关于java - 解析 RESTful PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26240603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com