java - 解析 RESTful PUT

标签 java rest

如何在 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/

相关文章:

java - 多个套接字导致 ConnectException : Connection refused: connect

java - 路径组件应为 '/'

c# - .NET Core API 项目中的分页

rest - 使用 SAS 的 REST 服务

java - 具有返回 void 的可运行参数的模拟方法

java - 需要帮助在 Java 版 Breakout 游戏中移动桨

java - 组织.postgresql.util.PSQLException : FATAL: database "pma-springbootdb"

php - 使用 PHP 在 Apache 2.4 上配置 RESTful VirtualHost

php - Laravel NotFoundHttpException 虽然路由存在

rest - 将 RPC 接口(interface)转换为 REST