制作具有Java示例的Python POST请求

时间:2017-09-27 12:38:12

标签: java python request httpconnection

我是Python的请求部分的新手,在这里遇到了一些麻烦.. 我在Java中有以下示例,它完美地运行...

public class teste {
    //The variable “text” must have all parameters. For instance, it could be
    //-b –m 4 –d –tree removeAll –t TEXT, TEXT, TEXT
    private static void sendPost(String text) throws Exception {

        //Sobek’s URL
        URL url = new URL("http://sobek.ufrgs.br/webservice/sobek.php");
        //Please pay attention to the constant value “data”
        String postData = "data= "+ URLEncoder.encode(text);
        byte[] postDataBytes = postData.getBytes();
        //Connect to the URL
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setDoOutput(true);
        //Send the information
        conn.getOutputStream().write(postDataBytes);
        //Read the server response
        Reader in = new BufferedReader(new
        InputStreamReader(conn.getInputStream(), "UTF-8"));
        for (int c; (c = in.read()) >= 0;)
            System.out.print((char)c);

    }
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String params = "-b –m 4 – tree removeAll -t TEXT";
        sendPost(params);
    }

}

但是我的软件是用Python编写的,所以我需要转换代码.. 我这样做了:

sobekURL = "sobek.ufrgs.br/webservice/sobek.php"

file_name = "Livro_MidiasNaEducacao_P1_rev3-cris2107.docx"

text = docx2txt.process(file_name)

parameters = "-b –m 4 – tree removeAll -t " + text

postData = "data=" + urllib.parse.quote_plus(parameters)
postDataBytes = bytearray(postData, 'utf8')

headers={
    "Content-type"  : "application/x-www-form-urlencoded",
    "Content-size"  : str(len(postDataBytes)) 
}
conn = http.client.HTTPConnection(sobekURL)
conn.request("POST",parameters,headers)
response=conn.getresponse()

print(response.status, response.reason)

data=response.read()

print(data)

conn.close()
conn.setRequestProperty();

我有很多错误,1by1解决它..但现在我明白了:

  

Traceback(最近一次调用最后一次):文件   " C:\用户\ alvarosps \工作空间\ SobekMiningCorpus \ AccessWebService.py&#34 ;,   第23行,在       conn.request(" POST",参数,标题)文件" C:\ Users \ alvarosps \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ http \ client.py",   第1239行,请求       self._send_request(method,url,body,headers,encode_chunked)文件   " C:\用户\ alvarosps \应用程序数据\本地\程序\的Python \ Python36 \ lib中\ HTTP \ client.py&#34 ;,   第1250行,在_send_request中       self.putrequest(方法,网址,**跳过)文件" C:\ Users \ alvarosps \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ http \ client.py",   第1117行,在putrequest       self._output(request.encode(' ascii'))UnicodeEncodeError:' ascii'编解码器不能编码字符' \ u2013'位置8:序数不在   范围(128)

有人可以帮忙吗?

编辑: 我已经将参数参数更改为parameters.encode(" utf-8")并解决了这个问题..但现在我得到了这个: **

  

Traceback(最近一次调用最后一次):文件   " C:\用户\ alvarosps \工作空间\ SobekMiningCorpus \ AccessWebService.py&#34 ;,   第23行,在       conn.request(" POST"," /",parameters.encode(' utf-8'),标题)文件   " C:\用户\ alvarosps \应用程序数据\本地\程序\的Python \ Python36 \ lib中\ HTTP \ client.py&#34 ;,   第1239行,请求       self._send_request(method,url,body,headers,encode_chunked)文件   " C:\用户\ alvarosps \应用程序数据\本地\程序\的Python \ Python36 \ lib中\ HTTP \ client.py&#34 ;,   第1285行,在_send_request中       self.endheaders(body,encode_chunked = encode_chunked)文件" C:\ Users \ alvarosps \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ http \ client.py",   第1234行,在终结者中       self._send_output(message_body,encode_chunked = encode_chunked)文件   " C:\用户\ alvarosps \应用程序数据\本地\程序\的Python \ Python36 \ lib中\ HTTP \ client.py&#34 ;,   第1026行,在_send_output中       self.send(msg)文件" C:\ Users \ alvarosps \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ http \ client.py",   第964行,发送       self.connect()文件" C:\ Users \ alvarosps \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ _ http \ client.py",   第936行,在连接中       (self.host,self.port),self.timeout,self.source_address)文件" C:\ Users \ alvarosps \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ socket.py",   第704行,在create_connection中       for get in getddrinfo(host,port,0,SOCK_STREAM):File" C:\ Users \ alvarosps \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ socket.py",   第743行,在getaddrinfo中       for _socket.getaddrinfo(host,port,family,type,proto,flags):socket.gaierror:[Errno 11001] getaddrinfo failed

**

这与发布的链接不一样,它使用不同的库,并且那里的答案没有解决任何问题..

0 个答案:

没有答案