0

我正在开发一个需要使用 curl 命令将文件加载到特定站点的项目。我希望使用 C# 来实现这一点。通过查看关于 SO 的类似问题,我没有发现任何似乎对我有用的东西,除非我做错了。

这是我在 Cygwin 中成功使用的 curl 命令:

curl -i -u user:password -F 'file1=@\Users\User\Desktop\test.jpg' -F 'json={"content": {"type": "text/html", "text": "<body><p>look at me, look at me</p></body>"}, "type": "document", "subject": "Document with Attachment"};type=application/json' http://someurl

现在我正在尝试使用 WebRequest 来完成这项工作,因为我已经读到这在其他帖子上是可能的。这就是我迄今为止所拥有的。

 WebRequest request = WebRequest.Create("http://someurl");
            request.PreAuthenticate = true;
            request.ContentType = "application/json";
            request.Method = "POST";
            string authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes("user:password"));
            request.Headers["Authorization"] = "Basic " + authInfo;

            //what do I set my buffer to?

            Stream reqstr = request.GetRequestStream();
            reqstr.Write(buffer, 0, buffer.Length);
            reqstr.Close();

            WebResponse response = request.GetResponse();

我应该在此处设置我的缓冲区以编写 curl 命令,该命令又应该发送文件 test.jpg?另外,我尝试使用 libcurl 也没有成功。非常感谢任何正确方向的指针!

4

1 回答 1

0

您可以先 POST json 文档描述,然后使用第二个请求 PUT 文件内容,或者使用 multipart/form-data 使用单个 POST 上传两者。

这篇文章有很好的例子:如何通过 API 向 Google Drive 添加/创建/插入文件?

于 2013-03-24T22:18:12.340 回答