Foxtable(狐表)用户栏目专家坐堂 → 网盘api的post调用失败,请帮忙测试


  共有4824人关注过本帖树形打印复制链接

主题:网盘api的post调用失败,请帮忙测试

帅哥哟,离线,有人找我吗?
luodang2050
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:586 积分:5685 威望:0 精华:0 注册:2014/5/21 10:30:00
网盘api的post调用失败,请帮忙测试  发帖心情 Post By:2016/7/11 20:49:00 [显示全部帖子]

已上传实例,请在命令窗口直接调用函数“Functions.Execute("复制文件测试")”测试,谢谢!

API说明文档详见:http://manual.seafile.com/develop/web_api_v2.1.html

 

预期实现效果,将资料库“中国”中的文件“/test/foo.c”复制到"/foo.c",个人测试报远程服务器400错误

 

 下载信息  [文件大小:   下载次数: ]
点击浏览该文件:seafile文件管理测试.table

Copy File

POST https://cloud.seafile.com/api2/repos/{repo-id}/file/?p=/foo.c

Request parameters

  • repo-id
  • p
  • operation
  • dst_repo
  • dst_dir

Sample request

curl -v -d "dst_repo=73ddb2b8-dda8-471b-b7a7-ca742b07483c&dst_dir=/&file_names=foo.c" -H 'Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd' https://cloud.seafile.com/api2/repos/c7436518-5f46-4296-97db-2fcba4c8c8db/file/?p=/foo.c

Sample response

...
< HTTP/1.1 200 OK
...
"success"


 回到顶部
帅哥哟,离线,有人找我吗?
luodang2050
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:586 积分:5685 威望:0 精华:0 注册:2014/5/21 10:30:00
  发帖心情 Post By:2016/7/12 9:29:00 [显示全部帖子]

检查了实在不知post哪里出现问题,测试账号项目里面已经填好了,即

网址:101.200.184.108

账号:admin@seafile.com     

密码:admin

 

 


 回到顶部
帅哥哟,离线,有人找我吗?
luodang2050
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:586 积分:5685 威望:0 精华:0 注册:2014/5/21 10:30:00
  发帖心情 Post By:2016/7/12 12:35:00 [显示全部帖子]

创建文件的调用post之前是已经调通了的,也跟老师建议的一样写法,就是复制文件的api测试不通,格式检查了没法问题,请看下复制文件的api


 回到顶部
帅哥哟,离线,有人找我吗?
luodang2050
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:586 积分:5685 威望:0 精华:0 注册:2014/5/21 10:30:00
  发帖心情 Post By:2016/7/12 12:53:00 [显示全部帖子]

已经调试成功了,老师果然经验丰富,文档有问题都看出来了,要不调个几天都调不出来,感谢

 回到顶部
帅哥哟,离线,有人找我吗?
luodang2050
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:586 积分:5685 威望:0 精华:0 注册:2014/5/21 10:30:00
  发帖心情 Post By:2016/7/21 13:32:00 [显示全部帖子]

还是上面的api,请教像这种带-H参数的应该怎么提交post?

Upload File

After getting the upload link, POST to this link for uploading files.

POST http://cloud.seafile.com:8082/upload-api/73c5d117-3bcf-48a0-aa2a-3f48d5274ae3

Errors

400 Bad request
440 Invalid filename
500 Internal server error

Sample request

curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" -F file=@test.txt -F filename=test.txt -F parent_dir=/ http://cloud.seafile.com:8082/upload-api/73c5d117-3bcf-48a0-aa2a-3f48d5274ae3

Sample response

"adc83b19e793491b1c6ea0fd8b46cd9f32e592fc"
[此贴子已经被作者于2016/7/21 13:34:39编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
luodang2050
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:586 积分:5685 威望:0 精华:0 注册:2014/5/21 10:30:00
  发帖心情 Post By:2016/7/21 14:32:00 [显示全部帖子]

是以下这几个参数怎么传递?

-F file=@test.txt -F filename=test.txt -F parent_dir=/     


 回到顶部
帅哥哟,离线,有人找我吗?
luodang2050
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:586 积分:5685 威望:0 精华:0 注册:2014/5/21 10:30:00
  发帖心情 Post By:2016/7/21 17:29:00 [显示全部帖子]

是否只需要改变文件、网址和头部,边界符不需要改动吧?书写规范具体哪里可参考,单看代码有点朦胧,有些参数不知道为什么存在,谢谢

 回到顶部
帅哥哟,离线,有人找我吗?
luodang2050
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:586 积分:5685 威望:0 精华:0 注册:2014/5/21 10:30:00
  发帖心情 Post By:2016/7/22 10:37:00 [显示全部帖子]

这是开源的项目,只有api规范,没有例子,不过已经搞定了,还是谢谢

 

参照规范:http://www.ietf.org/rfc/rfc1867.txt

并利用报文分析软件:fiddler,手动操作下,进行报文分析,发现有独特的报文边界,加以修正即可

[此贴子已经被作者于2016/7/22 10:39:07编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
luodang2050
  9楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:586 积分:5685 威望:0 精华:0 注册:2014/5/21 10:30:00
  发帖心情 Post By:2016/7/24 15:41:00 [显示全部帖子]

封装成函数如下:(测试账号及api文档见1楼及3楼)

'函数调用0=======================
'Functions.Execute("seafile_上传文件",ml0,fn)
'将本地文件上传到远程seafile,上传前需将文件复制到指定本地上传目录
'ml0:seafile上传相对目录,用于合成目录seafile_OAml & ml0
'fn:上传文件名
'函数调用1=======================
Dim fcn As String = "seafile_上传文件"
Dim ml0 As String = args(0)  '相对目录
Dim fn As String = args(1)  '文件名
Dim ml As String = seafile_OAml & ml0  'seaflie上传目录
Dim file As String = gs_OAup & ml0 & fn  '本地上传文件路径
Dim timeout As Integer = 10000
If args.length>2
    timeout = args(2)
End If
Dim id As String = seafile_ID
If args.length>3
    id= args(3)
End If
'ts(fcn,"id ",id )
'获取上传链接
Dim url As String = "api2/repos/" & id & "/upload-link/?p=" & ml
'ts(fcn,"url",url )
Dim str As String =  Functions.Execute("seafile_get",url)
'ts(fcn,"str",str)
'获取失败则返回错误
If Iserr(str)
    ts(fcn,"获取上传链接失败",str)
    Return str
End If
str=str.Trim("""")
'ts(fcn,"获取上传链接成功",str)
url = str
'获取cookieheader
Dim cookieheader As String
str = Functions.Execute("seafile_验证token")
If Iserr(str)
    ts(fcn,"seafile_验证token失败",str)
    Return ""
End If
cookieheader = "token " & seafile_token
'ts(fcn,"seafile_验证token成功",cookieheader )

'边界符
Dim boundary As String = "---------------------------" + DateTime.Now.Ticks.ToString("x")  '起始
Dim hl As Byte() = Encoding.ASCII.GetBytes(vbCrLf)  '换行
Dim beginBoundary As Byte() = Encoding.ASCII.GetBytes("--" & boundary & vbCrLf) '分割符
Dim endBoundary As Byte() = Encoding.ASCII.GetBytes(vbcrlf & "--" & boundary & "--" & vbcrlf) '结束
'文件流
Dim fileStream As new IO.FileStream(file, io.FileMode.Open, io.FileAccess.Read)
'定义变量
Dim responseContent As String
Dim memStream As New IO.MemoryStream
Dim header As String
Dim headerbytes As Byte()
'设置属性
Dim webRequest = System.Net.HttpWebRequest.Create(url)
webRequest.Method = "POST"
webRequest.Timeout = 10000
webRequest.C & boundary
webRequest.Headers.Add("Authorization",cookieheader)
'ts(fcn,"webRequest.ContentType",webRequest.ContentType)

'文件路径
memStream.Write(beginBoundary, 0, beginBoundary.Length)
header = "Content-Disposition: form-data;" & " name=""parent_dir" & """" & vbCrLf & vbCrLf
header &= ml
headerbytes = Encoding.UTF8.GetBytes(header)
memStream.Write(headerbytes, 0, headerbytes.Length)

'文件
memStream.Write(hl, 0, hl.Length)
memStream.Write(beginBoundary, 0, beginBoundary.Length)
header = "Content-Disposition: form-data;" & " name=""file" & """" & ";filename=" & """" & fn & """" & vbCrLf
header &= "Content-Type: application/octet-stream" & vbCrLf & vbCrLf
headerbytes = Encoding.UTF8.GetBytes(header)
memStream.Write(headerbytes, 0, headerbytes.Length)

Dim buffer(1024) As Byte
Dim bytesRead As Integer = fileStream.Read(buffer, 0, buffer.Length)
While bytesRead > 0
    memStream.Write(buffer, 0, bytesRead)
    bytesRead = fileStream.Read(buffer, 0, buffer.Length)
End While

'写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length)
webRequest.ContentLength = memStream.Length

try
    Dim requestStream = webRequest.GetRequestStream
   
    memStream.Position = 0
    Dim tempBuffer(memStream.Length - 1) As Byte
    memStream.Read(tempBuffer, 0, tempBuffer.Length)
    memStream.Close()
   
    requestStream.Write(tempBuffer, 0, tempBuffer.Length)
    requestStream.Close()
    Dim httpWebResponse As Net.WebResponse = webRequest.GetResponse
    Dim httpStreamReader As new Io.StreamReader(httpWebResponse.GetResponseStream, Encoding.GetEncoding("utf-8"))
    responseContent = httpStreamReader.ReadToEnd
    fileStream.Close()
    httpWebResponse.Close()
    webRequest.Abort()
    ts(fcn,"seafile_文件上传成功",responseContent)
    Return responseContent
Catch ex As Exception
     Return Functions.Execute("捕获错误输出",ex,"seafile_文件上传失败")
End Try

 

'函数调用0===================
'Functions.Execute("seafile_验证token")
'每次seafile_api调用需验证.成功返回token,失败返回错误代码
'引用seafile_token,seafile_url0,seafile_ok
'函数调用1===================
If seafile_ok = False OrElse seafile_token = ""
    Return Functions.Execute("seafile_获取token")
Else
    Dim url As String = "api2/auth/ping/"
    url = seafile_url0 & url
    Dim cookieheader As String = "Token " &  seafile_token
    try
        Dim rqst As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(url)
        rqst.Headers.Add("Authorization",cookieheader)
        Dim rsps As System.Net.HttpWebResponse = rqst.GetResponse
        Dim stm As System.IO.Stream = rsps.GetResponseStream()
        Dim reader As New System.IO.StreamReader(stm)
        Dim str As String = reader.ReadToEnd
        rsps.Close
        stm.Close
        reader.close
        If str="""" & "pong" & """"
            output.Show("Token验证成功:" & seafile_token)
            Return seafile_token
        Else
            Return Functions.Execute("seafile_获取token")
        End If
    Catch ex As Exception
        Functions.Execute("捕获错误输出",ex,"token验证失败!可能登录超时,将重新登录!")
        Return Functions.Execute("seafile_获取token")
    End try
End If
Return ""

 

'函数调用0===================
'Functions.Execute("seafile_获取token")
'成功则返回seafile_token,失败返回"error:***"
'主要用于初始登录验证,并更新seafile_token和seafile_ok
'引用:seafile_user,seafile_pas,seafile_ok,seafile_url0,seafile_token,seafile_ok
'函数调用1===================
Dim url0 As String = seafile_url0
Dim url As String = url0 & "api2/auth-token/"
'msgbox(url)
Dim post As String = "username=" & seafile_user & "&password=" & seafile_pas
Dim msg As String = post
Dim str As String
try
    Dim req = System.Net.WebRequest.Create(url)
    req.Method = "POST"
    req.C
    Dim aryBuf As Byte() = Encoding.GetEncoding("utf-8").GetBytes(msg)
    req.ContentLength = aryBuf.Length
    Dim writer = req.GetRequestStream()
    writer.Write(aryBuf, 0, aryBuf.Length)
    writer.Close()
    writer.Dispose()
    Dim pos = req.GetResponse
    Dim stm As System.IO.Stream = pos.GetResponseStream()
    Dim reader As New System.IO.StreamReader(stm)
    str = reader.ReadToEnd
    pos.Close
    stm.Close
    reader.close
    Dim data As object = Functions.Execute("解析json数组",str)
    Dim token As String = data.token
    output.Show("获取token:" & Token)
    seafile_token = Token
    seafile_ok =True
    Return seafile_token
Catch ex As Exception
    seafile_ok =False
    seafile_token = ""
    Return Functions.Execute("捕获错误输出",ex,"seafile登录验证失败!")
End Try

 

 

 

[此贴子已经被作者于2016/7/24 15:45:46编辑过]

 回到顶部