关于Invoke-RestMethod 的用法问题


PowerShell交流中心分类: Questions关于Invoke-RestMethod 的用法问题
0
pansal asked 5年 ago

新手求助~
想要爬取我们系统内网页的数据。如图所示,已经用chrome锁定了爬取内容–response中的一段json字符串

首先仿照站长的代码,通过cookie文件读取token

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
Get-Content .\cookienyf.txt  | foreach {

$line=$_ -split ‘/’ | select -First 1
$tokens=$_.Split(“`t”).TrimEnd()
$c=@{
 name=$tokens[0]
 value=$tokens[1]
 domain=$tokens[2]
}
 $cookie = New-Object System.Net.Cookie
 $cookie.Name=$c.name
 $cookie.Value=$c.Value
 $cookie.Domain=$c.domain
 $session.Cookies.Add($cookie)
}

然后使用Invoke-RestMethod 

$response = Invoke-RestMethod $myurl -Method post -ContentType “application/json”  -WebSession $session -OutFile output.json

原以为直接就可以得到json字符串,但根本没有。报错如下:
 

<?xml version=”1.0″ encoding=”UTF-8″?>
<root><flag>false</flag><message>解析Action出错</message><code>JUSTEP150013</code><reason></reason><stack>com.justep.exception.BusinessException: 编码: JUSTEP150013; 提示: 解析Action出错
at com.justep.exception.BusinessException.create(Unknown Source)
at com.justep.business.server.BusinessServerServlet.createActionJsonObject(Unknown Source)
at com.justep.business.server.BusinessServerServlet.getAction(Unknown Source)
at com.justep.business.server.BusinessServerServlet.execService(Unknown Source)
at com.justep.business.server.BusinessServerServlet.service(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at com.justep.x.bs.BusinessServerServlet.service(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at com.googlecode.psiprobe.Tomcat60AgentValve.invoke(Tomcat60AgentValve.java:30)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11NioProcessor.process(Http11NioProcessor.java:891)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:750)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:2283)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.json.JSONException: A JSONObject text must begin with ‘{‘ at character 0
at org.json.JSONTokener.syntaxError(JSONTokener.java:413)
at org.json.JSONObject.&lt;init&gt;(JSONObject.java:180)
at org.json.JSONObject.&lt;init&gt;(JSONObject.java:420)
… 22 more
</stack><data></data><messages>[“编码: JUSTEP150013; 提示: 解析Action出错”,”A JSONObject text must begin with ‘{‘ at character 0″]</messages></root>

 
不知哪位大神愿意指点一二?万分感谢!

×用微信扫描并分享
1 Answers
0
Mooser Lee 管理员 answered 5年 ago

你的Post请求已经生效了,至少说明登录成功了,因为你已经进入Action了。
出错误的原因是自己Post的数据的编码问题,或者你的Json格式不正确。
</stack><data></data><messages>[“编码: JUSTEP150013; 提示: 解析Action出错”,”A JSONObject text must begin with ‘{‘ at character 0″]</messages></root>