我找到了this ...沒有測試或完善......看看你是否可以用它做點什麼。
VB代碼:
Imports System.Security.Cryptography
Imports System.Collections.Specialized
Imports System.IO
Public Shared Sub HttpUploadFile(url As String, file As String, paramName As String, contentType As String, nvc As NameValueCollection)
Dim boundary As String = "---------------------------" + DateTime.Now.Ticks.ToString("x")
Dim boundarybytes As Byte() = Text.Encoding.ASCII.GetBytes((Convert.ToString(vbCr & vbLf & "--") & boundary) + vbCr & vbLf)
Dim wr As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
wr.ContentType = Convert.ToString("multipart/form-data; boundary=") & boundary
wr.Method = "POST"
wr.KeepAlive = True
wr.Credentials = Net.CredentialCache.DefaultCredentials
Dim rs As Stream = wr.GetRequestStream()
Dim formdataTemplate As String = "Content-Disposition: form-data; name=""{0}""" & vbCrLf & vbCrLf & "{1}"
For Each key As String In nvc.Keys
rs.Write(boundarybytes, 0, boundarybytes.Length)
Dim formitem As String = String.Format(formdataTemplate, key, nvc(key))
Dim formitembytes As Byte() = Text.Encoding.UTF8.GetBytes(formitem)
rs.Write(formitembytes, 0, formitembytes.Length)
Next
rs.Write(boundarybytes, 0, boundarybytes.Length)
Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""" & vbCrLf & "Content-Type: {2}" & vbCrLf & vbCrLf
Dim header As String = String.Format(headerTemplate, paramName, file, contentType)
Dim headerbytes As Byte() = Text.Encoding.UTF8.GetBytes(header)
rs.Write(headerbytes, 0, headerbytes.Length)
Dim fileStream As New FileStream(file, FileMode.Open, FileAccess.Read)
Dim buffer As Byte() = New Byte(4095) {}
Dim bytesRead As Integer = 0
While (InlineAssignHelper(bytesRead, fileStream.Read(buffer, 0, buffer.Length))) <> 0
rs.Write(buffer, 0, bytesRead)
End While
fileStream.Close()
Dim trailer As Byte() = Text.Encoding.ASCII.GetBytes((Convert.ToString(vbCrLf & "--") & boundary) + "--" & vbCrLf)
rs.Write(trailer, 0, trailer.Length)
rs.Close()
Dim wresp As WebResponse = Nothing
Try
wresp = wr.GetResponse()
Dim stream2 As Stream = wresp.GetResponseStream()
Dim reader2 As New StreamReader(stream2)
Catch ex As Exception
If wresp IsNot Nothing Then
wresp.Close()
wresp = Nothing
End If
Finally
wr = Nothing
End Try
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
原始C#代碼:
public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc) {
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream rs = wr.GetRequestStream();
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, paramName, file, contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) {
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
try {
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
} catch(Exception ex) {
if(wresp != null) {
wresp.Close();
wresp = null;
}
} finally {
wr = null;
}
}
是[此](https://msdn.microsoft.com/en-us/library/debx8sh9(V = VS .110).aspx?cs-save-lang = 1&cs-lang = vb#code-snippet-27)你在找什麼? – MrGadget
@MrGadget不,我使用WebClient.OpenRead發佈數據,這很好。我的問題是「用其他參數發送文件內容」,謝謝。 – nevin2007
你沒有充分告訴我們接收方...它期待什麼?可能是Web表單處理程序,通用數據處理程序,Web服務。假設我們對curl一無所知......告訴我們你實際上想要完成的事情。 – MrGadget