Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Groovy中的HTTPBuilder和MultipartEntity / multipart表单数据大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试模拟需要将一些INPUT / TEXT字段与文件中的数据组合在一起的HTTP POST.看起来我可以有一个或另一个,但不是两个?

在下面的代码段中,paramsToPost = [name:’John’,年龄:22]

@Grab(group='org.codehaus.groovy.modules.http-builder',module='http-builder',version='0.5.0')
Boolean doHttpPost(String url,Map paramsToPost,String fileContent) {
    HTTPBuilder http = new HTTPBuilder(url)
    def resp = http.request(Method.POST ) { req ->
        multipartentity mpe = new multipartentity()
        mpe.addPart "foo",new StringBody(fileContent)
        req.entity = mpe

        // body = paramsToPost // no such property
    }

    println "response: ${resp}"

    return true
}

有人有工作样品吗?

解决方法

刚刚让我的代码与旧的commons-httpclient-3.1.jar一起工作

(new HTTPBuilder(url)).request(Method.POST) { request ->
multipartentity mpe = new multipartentity(HttpMultipartMode.BROWSER_COMPATIBLE);
mpe.addPart('fileInput',new StringBody(params.fileInput))
if (params.fileInput=='file')
    mpe.addPart('file1',new InputStreamBody(uploadedFile.inputStream,uploadedFile.contentType,uploadedFile.originalFilename))
else if (params.fileInput=='text')
    mpe.addPart('fileText',new StringBody(params.fileText))
mpe.addPart('tags1',new StringBody(params.tags1)) 
request.entity = mpe
request.getParams().setParameter("http.connection.timeout",HTTP_TIMEOUT)
request.getParams().setParameter("http.socket.timeout",HTTP_TIMEOUT)
response.success = { resp,reader ->
    render(text : "Successfully uploaded file\n\n${reader.text}")
}
response.failure = { resp ->
  render (status: 500,text: "HTTP Failure Accessing Upload Service ${resp.statusLine}" )
}

希望这可以帮助

大佬总结

以上是大佬教程为你收集整理的Groovy中的HTTPBuilder和MultipartEntity / multipart表单数据全部内容,希望文章能够帮你解决Groovy中的HTTPBuilder和MultipartEntity / multipart表单数据所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: