1

我正在尝试模拟这个 java 代码

            public static void uploadFile(String filename, String systemID){
            try{
                    String createNew = "false";

                    //check for backup files to know if we should make a new file on the server
                    File f = new File(filename + ".1");
                    if(f.exists()){
                            createNew = "true";
                            f.delete();
                    }

                    HttpURLConnection httpUrlConnection = (HttpURLConnection)new URL(uploadURL).openConnection();
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setRequestMethod("POST");
            httpUrlConnection.setRequestProperty("Content-encoding", "deflate");
            httpUrlConnection.setRequestProperty("Content-type", "application/octet-stream");
            java.io.OutputStream os = httpUrlConnection.getOutputStream();
            Thread.sleep(1000);

            String fileData = IOUtils.toString(new FileReader(filename));

            String request = "filedata=" + fileData + "&filename=" + filename + "&systemid=" + systemID + "&createNew=" + createNew;

            DeflaterOutputStream deflate = new DeflaterOutputStream(os);
            deflate.write(request.getBytes());
            deflate.flush();
            deflate.close();

            os.close();
            BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));

            String s = null;
            while ((s = in.readLine()) != null) {
                System.out.println(s);
            }
            in.close();
            //fis.close();
            }catch(Exception e){
                    e.printStackTrace();
            }
            static String uploadURL = "http://vp-creations.com/utilities/fileupload.php";

在使用请求的python中,我认为我错过了一些简单的东西,因为我无法从服务器获得正确的响应

import requests
url = 'http://vp-creations.com/utilities/fileupload.php'
files = {'file': open('C:\\etc\\guitartab.txt', 'rb')}
headers = {'Content-encoding': 'deflate', 'Content-type': 'application/octet-stream'}

payload = {'filedata=': 'foo', 'filename=': 'bar', 'systemid=' : 'fooe', 'createNew=' : 'false'}

r = requests.post(url, files=files, headers=headers, data=payload)

我不断收到来自服务器的响应是 {"response":"error","comment":"missing at least one parameter"}' 有什么帮助吗?

4

2 回答 2

0

当您将字典传递给 的data参数时requests.post,它将被形式编码。我对 Java 代码并不完全熟悉,但看起来它实际上是在使用编码String作为请求的数据。为了实现与 相同的事情requests,传递一个字符串作为data参数,如下所示:

data = 'filedata=foo&filename=bar&systemid=&fooe&createNew=false'
r = requests.post(url, files=files, headers=headers, data=data)

请参阅此处文档中的类似示例。

于 2013-07-02T18:20:30.300 回答
0

您需要尝试更改以下内容。

首先,data在 POST 正文中发送给定的字典。如果这确实是您想要的,则需要从字典中删除 '=' 元素:

payload = {'filedata': 'foo', 'filename': 'bar', 'systemid' : 'fooe', 'createNew' : 'false'}

其次,您添加了一对不正确的标题。当您使用 Requests 的file参数时,Requests 会将正文数据(包括您的文件)准备为multipart/form-data'. 然后,您用您的 覆盖该 Content-Type application/octet-stream,这不是正文的形式。此外,您声称您的身体数据是使用 压缩的deflate,但事实并非如此。

试试下面的代码:

import requests
url = 'http://vp-creations.com/utilities/fileupload.php'
files = {'file': open('C:\\etc\\guitartab.txt', 'rb')}

payload = {'filedata': 'foo', 'filename': 'bar', 'systemid' : 'fooe', 'createNew' : 'false'}

r = requests.post(url, files=files, data=payload)
于 2013-07-03T12:44:36.207 回答