我正在使用新的Python Requests库来发出http请求.我从服务器获取cookie作为文本.如何将其变成CookieJar带有cookie的a ?
我有一个ASP.NET Web API端点,其控制器操作定义如下:
[HttpPost]
public HttpResponseMessage Post([FromBody] object text)
如果我的帖子请求正文包含纯文本(即不应该被解释为json,xml或任何其他特殊格式),那么我认为我可以在我的请求中包含以下标题:
Content-Type: text/plain
但是,我收到错误:
No MediaTypeFormatter is available to read an object of type 'Object' from content with media type 'text/plain'.
如果我将控制器操作方法签名更改为:
[HttpPost]
public HttpResponseMessage Post([FromBody] string text)
我得到一个略有不同的错误消息:
No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'text/plain'.
如何使用python库请求处理异常?例如,如何检查PC是否连接到互联网?
当我尝试
try:
    requests.get('http://www.google.com')
except ConnectionError:
    # handle the exception
它给我的错误名称ConnectionError没有定义
我想知道来自第三方网站的传入HTTP_REQUEST调用是否来自我定义的域列表.
我知道HTTP_REFERER可以用来找出第三方域的位置,但它不够安全.人们可以欺骗它或使用Telnet伪造它.
那么,HTTP_ORIGIN怎么样?是从所有浏览器发送的吗?它安全吗?
此外,人们可以在HTTP_REQUEST调用中伪造REMOTE_ADDR吗?
我有以下代码:
Image tmpimg = null;
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
return Image.FromStream(stream);
在我输入的最后一行Image.,FromStream不在列表中.我能做什么?
我正在使用python请求实现私有HTTP-API的客户端库.API(我无法控制)期望参数按特定顺序排列,但python-requests不会将排序的dict作为参数.
这是我试过的:
import requests
from django.utils.datastructures import SortedDict
params = SortedDict()
params['s'] = 'value1'
params['f'] = 'value2'
requests.get('https://example.org/private_api', params=params)
#performs request as https://example.org/private_api?f=value1&s=value2 
这就是我想要避免的:
requests.get('https://example.org?{0}'.format(urlencode(params)))
我正在尝试解析使用Python Requests库完成的HEAD请求的结果,但似乎无法访问响应内容.
根据文档,我应该能够访问requests.Response.text中的内容.这对我的GET请求很好,但在HEAD请求时返回None.
GET请求(工作)
import requests
response = requests.get(url)
content = response.text
content = <html>...</html>
HEAD请求(无内容)
import requests
response = requests.head(url)
content = response.text
content = None
编辑
好的我已经很快意识到HEAD请求不应返回仅内容标题的答案.但这是否意味着,要访问在<head>页面标签中找到的东西,比如<link>和<meta>标签,那么必须获取整个文档?
我正在使用Node.js并表达.
我想限制HTTP请求的大小.假设有人向我发送超过2 MB的HTTP请求,那么我立即停止请求.我查看了代码,我想如果我改变核心,我就可以做到.但是,有没有办法设置max_request_size这样的东西?
这与我的第二个问题有关.我正在使用express来获取上传的文件req.files./tmp一旦文件大小超过某个文件大小,有没有办法停止将文件写入文件夹(这是默认的上载行为)?
在以下代码中:
#!/usr/local/bin/python
import json
APPLICATION_NAME = 'cc9226315643df89-36bf02429075329d0ba36748360d050c'
HEADERS1 = json.dumps(dict(Destination = u"/api/af/latest/applications/%s/rulesets" % (APPLICATION_NAME)))
print "Headers1 is %s" % (HEADERS1)
HEADERS2 = {'Destination': '/api/af/latest/applications/%s/rulesets' % (APPLICATION_NAME)}
print "Headers2 is %s" % (HEADERS2)
我得到以下输出:
Headers1 is {"Destination": "/api/af/latest/applications/cc9226315643df89-36bf02429075329d0ba36748360d050c/rulesets"}
Headers2 is {'Destination': '/api/af/latest/applications/cc9226315643df89-36bf02429075329d0ba36748360d050c/rulesets'}
但是当我尝试使用requests()在REST调用中使用HEADER1或HEADER2时,我得到了非常不同的结果:
SERVER_URL = 'http://1.1.33.109:8087%s' % (APP_PATH)
REQ_DATA = None
print "Headers are: ", HEADERS
print "SERVER_URL is: ", SERVER_URL
print "Request Data is:", REQ_DATA
print ""
RESPONSE = requests.request(
    'MOVE', 
    SERVER_URL, 
    auth = ('admin', 'admin'), 
    verify = False, 
    data …在Powershell v3.0中,我想从HTTP GET返回响应代码,例如200 OK或500 Internal Server Error.(这是为了进行黑客部署,对已部署的站点进行快速预热并查看它是否有效,这是一种小型验收测试.状态代码确实是我想要的.)
违背我的意愿,HttpWebRequest.GetResponse收到错误时会抛出错误500 Internal Server Error.这很烦人,因为在我的用例中它对我来说并不是一个错误.无论如何,我认为我可以捕获异常并仍然剥离底层响应代码,但我遇到了麻烦.
这是一些几乎可以工作的代码:
function WebResponseStatusCode (
   [Parameter(Mandatory=$true)][string] $url
) {
    $req = [system.Net.HttpWebRequest]::Create($url)
    try {
        $res = $req.GetResponse();
        $statuscode = $res.statuscode;
    }
    catch [System.Net.WebException] {
        #the outer error is a System.Management.Automation.ErrorRecord
        Write-Host "error!"
        return = $_.Response.statuscode; #nope
    }
    finally {
        if (!($res -eq $null)) {
           $res.Close();
        }
    }
   return $statuscode;
}
问题当然是$_没有Response财产.也不是$_.InnerException,即使是演员:
return [System.Net.WebException]($_.InnerException)
我玩过$_ | …
http-request ×10
python ×5
c# ×1
content-type ×1
cookies ×1
dictionary ×1
dns ×1
exception ×1
express ×1
file-upload ×1
head ×1
http-post ×1
httpresponse ×1
node.js ×1
php ×1
powershell ×1
rest ×1
size ×1
string ×1
text ×1