我正在尝试使用 cfhttp 从自动下载 url 获取文件。我正在使用以下代码:
<cfhttp method="get" url="http://www.example.com/getfile" path="E:/" file="abc.csv">
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我将文件类型指定为 CSV,因此我能够获取文件,但文件类型可以更改。我试图CFHTTP.MIMETYPE获取文件类型并像这样使用:
<cfhttp method="get" url="http://www.example.com/getfile">
<cffile action="write" file="E:/abc.#listLast(cfhttp.MIMETYPE,'/')#" output="#cfhttp.FileContent#">
Run Code Online (Sandbox Code Playgroud)
这适用于 CSV 和 XML 文件。但我希望它也适用于 Excel 文件。
请帮忙。提前致谢。
我每天都面临这个问题.我有一个应用程序通过cfhhtp呼叫/请求从夜间计划作业中的各种应用程序中获取大量数据.这里的问题是它调用'扩展的范围列表'来捕获数据,不幸的是我们不能限制范围:(任务中设置的超时是9000秒(已经很高),但它仍然表示超时在cfhttp标签处
"请求已超过允许的时间限制Tag:cfhttp".
我不知道它是如何cfhttp工作的,但应该有一些解决方案,如果它长时间捕获数据并从各种范围捕获,它不应该抛出错误并继续工作直到最后一个请求.
<cfset dynVarName = "funded" & bizforShort>
<cfif structKeyExists(variables,dynVarName)>
<cfset howManyCustomScopes = listLen(structkeylist(variables[dynVarName],"|" ),"|" )>
<cfmodule template="#Request.Library.CustomTags.VirtualPath#Scheduler_LogDetail.cfm"
Step="Funded Level Cache" Detail="Custom Scopes to be cached: #howManyCustomScopes#"
LogData=""></cfmodule>
<cfloop collection="#variables[dynVarName]#" item="t">
<cfset tempurl = variables[dynVarName][t]["url"]>
<cfset tempurl = tempurl & "&retainCache=1">
<cfoutput>
<cfhttp url="#tempurl#" method="GET" resolveurl="false" timeout="9000">
#tempurl#<br>
<cfset scopesCachedCounter = scopesCachedCounter + 1>
<cfmodule template="#Request.Library.CustomTags.VirtualPath#Scheduler_LogDetail.cfm" Step="Funded Scopes Cache" Detail="#scopesCachedCounter#.- #t#" LogData="#tempurl#"></cfmodule>
</cfoutput>
</cfloop>
</cfif>
Run Code Online (Sandbox Code Playgroud)
不是:页面有一个"包含",它从中捕获范围.
我是Coldfusion的新手,无法理解为什么我在使用cfhttp进行的http请求中添加几个标题时遇到了很多麻烦.我正在尝试使用cfscript编写请求,并且从我读过的所有内容中我应该能够简单地执行:
httpService.addParam(type="header", name="Content-Type", value="application/json");
Run Code Online (Sandbox Code Playgroud)
要么
httpService.addParam(type="header", name="Authorization", value=local.authPasscode);
Run Code Online (Sandbox Code Playgroud)
但传出请求似乎不包含这些标头.我可以告诉它不起作用,因为:
writeDump(GetHttpRequestData()),它显示了请求的所有细节(此输出根本不显示Authorization标头,并将Content-Type标头显示为:多部分/格式的数据; 边界= ---- WebKitFormBoundaryZs9TyOQV02N3fQop
我似乎也很难以类似的方式添加消息正文,所以我认为我做错了addParam().我是这样尝试的:
httpService.addParam(type="body", value="hello");
Run Code Online (Sandbox Code Playgroud)
同样,我得到的响应有一堆缺失的字段错误,输出writeDump(GetHttpRequestData())显示缺少的正文.
我应该能够以addParam()这种方式使用吗?
完整代码:
httpService = new http();
httpService.setMethod("POST");
httpService.setUrl(application.config.beanstream.postURL);
local.authPasscode = "Passcode " & ToBase64(application.config.beanstream.merchantid & ":" & application.config.beanstream.APIPasscode, "utf-8");
// Set headers.
httpService.addParam(type="header", name="Authorization", value=local.authPasscode);
httpService.addParam(type="header", name="Content-Type", value="application/json");
// Construct the message body.
local.body = {
"test": "hello"
};
httpService.addParam(type="body", value=SerializeJSON(local.body));
writeDump(GetHttpRequestData());
local.result = httpService.send().getPrefix();
Run Code Online (Sandbox Code Playgroud)
更新:
好的,所以我把我postURL改为另一个本地页面,在那个页面上,我GetHttpRequestData()用来将方法,协议,标题和内容全部记录到文件中.这是我得到的:
POST
HTTP/1.1 …Run Code Online (Sandbox Code Playgroud) 有没有其他人能够用CFHTTP删除purge_everything?我似乎无法通过ColdFusion CFHTTP成功清除CloudFlare区域的缓存.但我能够做其他事情,如列表区等.所以我知道我可以成功CFHTTP到CloudFlare的API.
这是curl命令,有效:
curl -svX DELETE -H 'X-Auth-Email: a@b.c' -H 'X-Auth-Key: XYZ' https://api.cloudflare.com/client/v4/zones/xxxxxxx/purge_cache -H 'Content-Type: application/json' --data '{"purge_everything":true}'
Run Code Online (Sandbox Code Playgroud)
返回的错误是:
{"success":false,"errors":[{"code":1012,"message":"请求必须包含\"purge_everything \"或\"files \"或\"tags"}],"消息 ":[]," 结果":空}
我尝试了很多代码组合......这些是我尝试过的不同变量:
<cfset stFields = '{"purge_everything":true}'>
<cfset stFieldsJson = {"purge_everything":true}>
<cfset stFieldsJson2 = {
"fields" : {
"purge_everything" : true
}
}>
<cfset stFieldsJson3 = {
"purge_everything" : true,
"fields" : {
"purge_everything" : true
}
}>
<cfset tmp = {} />
<cfset tmp['purge_everything'] = true />
Run Code Online (Sandbox Code Playgroud)
......这里是我做过的一些不同的电话组合......
<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare" charset="utf-8">
<cfhttpparam type="header" name="X-Auth-Email" value="a@b.c"> …Run Code Online (Sandbox Code Playgroud) fileExists远程文件检查是否使用cfhttp head?
在检查远程文件时,我担心fileExists超过CFHTTP的速度
<cfset imgExist = FileExists('https://qph.fs.quoracdn.net/main-qimg-e8b07145ae25974d4902168818241326.webp') >
<cfdump var="#imgExist#">
-- Returns Yes --
Run Code Online (Sandbox Code Playgroud)
FileExists函数是否使用CFHTTP头?
<cfhttp method="head" url="someimage" resolveurl="no" throwonerror="no" timeout="2" />
Run Code Online (Sandbox Code Playgroud)
检查远程文件是否存在时,FileExists优于CFHTTP的优势是什么?
在服务器负载方面,FileExists也比CFHTTP更好吗?
我正在尝试将API调用编码为在线测试公司。他们提供了PHP和cURL的示例调用,我需要使用来在ColdFusion 11中实现该调用<CFHTTP>。到目前为止,我的尝试失败了。我从他们的服务器/ API得到的唯一响应是:
Statuscode =“连接失败。状态代码不可用。”
和
ErrorDetail =“ I / O异常:握手期间远程主机关闭了连接”。
如果工作正常,我将获得一个JSON字符串,详细说明计算的分数。请注意,出于安全原因,在下面的代码中,我更改了一些值,但它是原始代码。任何建议或意见,将不胜感激,谢谢。
这是ColdFusion / cfhttp代码:
<cfoutput>
<cfset sdata = [
{
"customerid" = "ACompany",
"studentid" = "test",
"form" = "X",
"age" = "18.10",
"norms" = "grade",
"grade" = "2"
},
{
"scores" = [
{"subtest"="math","score"="34"},
{"score"="23","subtest"="lang"},
{"score"="402","subtest"="rcomp"}
]
}
]>
<!--- create JSON string for request --->
<cfset jsdata = serializeJSON(sdata)>
<!--- make the call --->
<cfhttp method="Get" url="https://www.APIwebsite.php" timeout="10" result="varx">
<cfhttpparam type="header" name="Content-Type" value = "application/json; charset=utf-8"/> …Run Code Online (Sandbox Code Playgroud) 我想使用 lucee/coldfusion 发出 API 请求。
我像这样设置我的令牌请求:
cfhttp(
url="[myurl]"
method="POST"
result="token"
) {
cfhttpparam(type="header" name="host" value="[url]");
cfhttpparam(type="body" name="client_id" value="[id]");
cfhttpparam(type="body" name="client_secret" value="[secret]");
cfhttpparam(type="body" name="grant_type" value="[credentials]");
cfhttpparam(type="body" name="scope" value="[url]");
};
Run Code Online (Sandbox Code Playgroud)
但错误消息告诉我需要包含“grant_type”,所以我的正文似乎没有正确发送。
有人可以帮我吗?
编辑:
我也尝试过这个:
var body = {
"host": "[url]",
"client_id": "[id]",
"client_secret": "[secret]",
"grant_type": "[credentials]",
"scope": "[url]"
}
// Token
cfhttp(
url="[url]"
method="POST"
result="token"
) {
cfhttpparam(type="header" name="host" value="[url]");
cfhttpparam(type="body" value="#body.toJson()#");
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试让CFHTTP与我在测试服务器上为测试目的而创建的域进行通信.域的地址是"mydomain.example.com".每次我尝试使用cfhttp连接我得到一个错误说明:
Your requested host "mydomain.example.com" could not be resolved by DNS.
Run Code Online (Sandbox Code Playgroud)
我已经在windows hosts文件中添加了该条目.
mydomain.example.com 127.0.0.1
Run Code Online (Sandbox Code Playgroud)
我还确保java.net.InetAddress可以通过在coldfusion页面中执行以下操作来解析域:
<cfset loc.javaInet = createObject("java","java.net.InetAddress")>
<cfset loc.dnsLookup = loc.javaInet.getByName("mydomain.example.com")>
Run Code Online (Sandbox Code Playgroud)
为了回来
mydomain.example.com/127.0.0.1
Run Code Online (Sandbox Code Playgroud)
我甚至尝试启动和停止coldfusion服务,并将runtime\jre\lib\security\java.security中networkaddress.cache.ttl的值更改为0.
我迷失了为什么一切似乎都在jre级别解决,而不是在cfhttp级别.有任何想法吗???
我有一个登录帐户的API,然后通过CFHTTP请求搜索我想要出价的项目,如下所示.
Search.cfm:
<cfinclude template="logMeIn.cfm" />
<cfhttp url="https://utas.fut.ea.com/ut/game/fifa14/transfermarket?maxb=1800000&start=0&num=16&type=player&maskedDefId=158023" method="POST" result="Get">
<cfhttpparam type="header" name="Cache-Control" value="no-cache" />
<cfhttpparam type="header" name="Host" value="utas.fut.ea.com" />
<cfhttpparam type="header" name="Referer" value="http://cdn.easf.www.easports.com/soccer/static/flash/futFifaUltimateTeamPlugin/FifaUltimateTeam.swf" />
<cfhttpparam type="header" name="X-HTTP-Method-Override" value="GET" />
<cfhttpparam type="header" name="X-UT-PHISHING-TOKEN" value="#ListLast(Session.searchAccount40PhishingKey,'=')#" />
<cfhttpparam type="header" name="X-UT-SID" value="#Session.searchAccount40SessionKey#" />
</cfhttp>
Run Code Online (Sandbox Code Playgroud)
logMeIn.cfm:
<cfscript>
Variables.email = "XYZ@example.com";
Variables.password = "password";
Variables.secretAnswer = "secretAnswer";
Session.searchAccountGamertag = "WirierJupiter11";
Variables.doReload = 0;
Variables.deviceView = "device=not_mobile";
</cfscript>
<cfhttp url="http://www.easports.com/uk/fifa/football-club/ultimate-team" method="GET" result="stage1" redirect="false">
<cfhttpparam type="header" name="Accept" value="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" />
<cfhttpparam type="header" name="Accept-Encoding" value="gzip, deflate" />
<cfhttpparam type="header" name="Accept-Language" …Run Code Online (Sandbox Code Playgroud) 我一直允许用户通过在线表单上传文件,如下所示:
<form action="upload.htm" enctype="multipart/form-data" name="upload_form" method="post">
<input type="file" name="upfile">
<input type="submit" name="upload" value="Upload Photos">
</form>
Run Code Online (Sandbox Code Playgroud)
然后在后端,cffile 将上传文件:
<cffile action="upload" destination="#currentpath#" accept="image/jpeg, image/gif, image/png" fileField="form.upfile" nameconflict="makeunique">
Run Code Online (Sandbox Code Playgroud)
现在我想做自动化,以便图像文件不必位于用户的计算机中,而是来自网络目的地,例如http://www.sample.com/images/myimage.jpg而不是 c:/images/我的图片.jpg
我试过这个:
<cfhttp method="get" url="http://www.example.com/images/myimage.jpg" resolveurl="Yes" throwOnError="Yes">
<cfif cfhttp.mimeType eq "image/jpeg">
<cfset currentpath = expandpath('/test/')>
<cffile action="upload" destination="#currentpath#" accept="image/jpeg, image/gif, image/png" fileField="#cfhttp.fileContent#" nameconflict="makeunique">
</cfif>
Run Code Online (Sandbox Code Playgroud)
但是,它给了我一个错误:
无效的内容类型:''。文件上传操作需要表单使用 enctype="multipart/form-data"
我没有使用表单上传,但它似乎需要一个表单字段。
所以我试过这个:
<cfhttp method="get" url="http://www.example.com/images/myimage.jpg" resolveurl="Yes" throwOnError="Yes">
<cfif cfhttp.mimeType eq "image/jpeg">
<cfset currentpath = expandpath('/test/')>
<cffile action="write" output="#cfhttp.fileContent#" file="#currentpath#/someimage.jpg">
</cfif>
Run Code Online (Sandbox Code Playgroud)
这个写出一个名为 someimage.jpg 的“文件”,但输出不是一个 jpg 文件,而是一些无法识别的文件。使用 …
cfhttp ×10
coldfusion ×10
cfml ×3
cffile ×2
cloudflare ×1
curl ×1
http-delete ×1
httprequest ×1
httpresponse ×1
lucee ×1
php ×1
upload ×1
web-services ×1