使用GET方法使用IronPython进行Web请求

时间:2017-02-08 11:04:11

标签: .net http ironpython

我在GET下使用 IronPython 代码下面的请求网络方法。它使用 POST 方法。然后我将post方法更改为GET request.Method = "GET",然后在之后将此示例更改为GET方法。

from System.Net import WebRequest
from System.IO import StreamReader
from System.Text import Encoding

def UrlOpen(uri, parameters=None):
    request = WebRequest.Create(uri)
    if parameters is not None:
        request.ContentType = "application/x-www-form-urlencoded"
        #request.Method = "POST" #work for post
        request.Method = "GET"   #not work for get method
        bytes = Encoding.ASCII.GetBytes(parameters)
        request.ContentLength = bytes.Length
        reqStream = request.GetRequestStream()
        reqStream.Write(bytes, 0, bytes.Length)
        reqStream.Close()

    response = request.GetResponse()
    result = StreamReader(response.GetResponseStream()).ReadToEnd()
    return result

print(UrlOpen("http://localhost:89/api/","data=1"))

仅用于Web服务的PHP代码:

<?php
echo $_POST["data"];        
?>

更改request.Method = "GET"

后,它在Python控制台上显示为以下错误
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<string>", line 9, in UrlOpen
SystemError: Cannot send a content-body with this verb-type.

0 个答案:

没有答案