使用 multipart/form-data (Confluence) 在 C# 中上传附件

Uploading an attachment in C# using multipart/form-data (Confluence)

我一直在尝试通过 Confluence Documentation on posting an attachment post 汇合页面上的附件。 我尝试使用以下代码上传附件(.txt 文件或任何图像文件)。我还使用了 curl 命令,它适用于本地安装的 confluence,但在线上传时出现 "certifcates missing" 错误。 我目前正在处理的 C# 代码片段是:

string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Credentials = CredentialCache.DefaultCredentials;
req.Headers.Add("X-Atlassian-Token", "nocheck");
req.Method = "POST";
req.ContentType = "multipart/form-data; boundary=" + boundary;
string contentDisp = "Content-Disposition: form-data; name=\"file\"; filename=\"foo.txt\"";
string contentType = "Content-Type: text/plain";
string fullContents = "{0}\r\n{1}\r\n{2}\r\n\r\n{3}\r\n{4}--", boundary, contentDisp,contentType, Encoding.Default.GetString(contents), boundary;
req.ContentLength = fullContents.Length;
using (StreamWriter reqStream = new StreamWriter(req.GetRequestStream()))
{
reqStream.Write(fullContents);
reqStream.Flush();
reqStream.Close();
}
var httpResponse = (HttpWebResponse)req.GetResponse(); 

我得到的错误是:"An unhandled exception of type 'System.Net.WebException' occurred in System.dll. Additional information: The remote server returned an error: (400) Bad Request."

希望这会有所帮助

我将其张贴在 VB 以防出现翻译问题。您可以在这里转换它:

CodeConverter

Dim nvc As New System.Collections.Specialized.NameValueCollection() 'NOTE: Nothing to send at this time but must be initialized and passed
Dim strTargetUri As String = "https://yourserver.com/rest/api/content/1234567890/child/attachment"
Dim attachmentItem As String = "C:\folder\yourfile.png"
AddImageAttachment(strTargetUri, attachmentItem, "file", "image/jpeg", nvc)

我将其分解为 3 个步骤

Public Shared Sub AddImageAttachment(ByVal uri As String, ByVal filePath As Hub.AttachmentElement, ByVal fileParameterName As String, ByVal contentType As String, ByVal otherParameters As Specialized.NameValueCollection)

    ServicePointManager.ServerCertificateValidationCallback = Function(sender, certificate, chain, sslPolicyErrors) True

    Dim strFileName As String = filePath.SourceFileName
    Dim strPathAndFileName As String = filePath.SourcePathAndFileName

    Dim boundary As String = "---------------------------" & DateTime.Now.Ticks.ToString("x")
    Dim newLine As String = System.Environment.NewLine
    Dim boundaryBytes As Byte() = Text.Encoding.ASCII.GetBytes(newLine & "--" & boundary & newLine)
    Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
    'request.Proxy = New WebProxy("127.0.0.1", 8888)    'Proxy Entry for Tracing with Fiddler

    request.ContentType = "multipart/form-data; boundary=" & boundary
    request.Headers.Add("X-Atlassian-Token: no-check")
    request.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(credLogonID & ":" & credPassword)))
    request.Method = "POST"
    request.Credentials = Net.CredentialCache.DefaultCredentials

    Try

        Using requestStream As Stream = request.GetRequestStream()
            Dim formDataTemplate As String = "Content-Disposition: form-data; name=""{0}""{1}{1}{2}"
            If otherParameters.Keys.Count > 0 Then
                For Each key As String In otherParameters.Keys
                    requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
                    Dim formItem As String = String.Format(formDataTemplate, key, newLine, otherParameters(key))
                    Dim formItemBytes As Byte() = Text.Encoding.UTF8.GetBytes(formItem)
                    requestStream.Write(formItemBytes, 0, formItemBytes.Length)
                Next key
            End If

            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)

            Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2}Content-Type: {3}{2}{2}"
            Dim header As String = String.Format(headerTemplate, fileParameterName, strFileName, newLine, contentType)
            Dim headerBytes As Byte() = Text.Encoding.UTF8.GetBytes(header)
            requestStream.Write(headerBytes, 0, headerBytes.Length)

            Dim byteImage As Byte() = GetImage(strPathAndFileName)

            requestStream.Write(byteImage, 0, byteImage.Length)

            Dim trailer As Byte() = Text.Encoding.ASCII.GetBytes(newLine & "--" + boundary + "--" & newLine)
            requestStream.Write(trailer, 0, trailer.Length)
        End Using

        Dim response As WebResponse = Nothing

    Catch e As Net.WebException
        MsgBox(e.Message)
    End Try
End Sub

获取图像

Private Shared Function GetImage(ByVal URL As String) As Byte()

    GetImage = Nothing

    ServicePointManager.ServerCertificateValidationCallback = Function(sender, certificate, chain, sslPolicyErrors) True
    Dim Request As HttpWebRequest = Nothing
    Dim Response As HttpWebResponse = Nothing
    Try
        Request = WebRequest.Create(URL)
        'request.Proxy = New WebProxy("127.0.0.1", 8888)    'Proxy Entry for Tracing with Fiddler
        Request.Credentials = CredentialCache.DefaultCredentials
        Response = CType(Request.GetResponse, WebResponse)
        If Request.HaveResponse Then
            If Response.StatusCode = Net.HttpStatusCode.OK Then
                GetImage = convertStreamToByte(Response.GetResponseStream)
            End If
        End If
    Catch e As WebException
        MsgBox(e.Message)
    End Try

    Return GetImage

End Function

和字节转换器

Private Shared Function convertStreamToByte(ByRef incomingStream As Stream) As Byte()

    Dim result As Byte()
    Dim buffer As Byte() = New Byte(4095) {}
    Using memoryStream As New MemoryStream()
        Dim count As Integer = 0
        Do
            count = incomingStream.Read(buffer, 0, buffer.Length)
            memoryStream.Write(buffer, 0, count)
        Loop While count <> 0
        result = memoryStream.ToArray()
    End Using
    Return result

End Function

我看看能不能找到我用来把它放在一起的一些链接