2010-10-08 32 views
0

我有以下代碼C#:獲取的HttpWebRequest的請求流給ProtocolViolationException

var postData = ""; 
var req = HttpWebRequest.Create("http://www.imageshack.us/upload_api.php"); 
req.ContentType = "multipart/form-data"; 
postData += "key=xxx&"; 
postData += "type=base64&"; 

// get base64 data from image 
var fs = new FileStream(@"D:\tmp\WpfApplication1\WpfApplication1\Images\Icon128.gif", FileMode.Open, FileAccess.Read); 
byte[] bytes = new byte[fs.Length]; 
fs.Read(bytes, 0, Convert.ToInt32(fs.Length)); 
string encoded = Convert.ToBase64String(bytes); 
postData += "fileupload=" + encoded; 

byte[] reqData = Encoding.UTF8.GetBytes(postData); 
Stream dataStream = req.GetRequestStream(); // error triggered here 

堆棧跟蹤:

System.Net.ProtocolViolationException was unhandled 
    Message=Cannot send a content-body with this verb-type. 
    Source=System 
    StackTrace: 
     at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream) 
     at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) 
     at System.Net.HttpWebRequest.GetRequestStream() 
     at WpfApplication1.ImageUpload.Button_Click(Object sender, RoutedEventArgs e) in D:\tmp\WpfApplication1\WpfApplication1\ImageUpload.xaml.cs:line 44 
     at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
     at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
     at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 
     at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) 
     at System.Windows.Controls.Primitives.ButtonBase.OnClick() 
     at System.Windows.Controls.Button.OnClick() 
     at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 
     at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e) 
     at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
     at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
     at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
     at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
     at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) 
     at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) 
     at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
     at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
     at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
     at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
     at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 
     at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) 
     at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) 
     at System.Windows.Input.InputManager.ProcessStagingArea() 
     at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) 
     at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) 
     at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) 
     at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
     at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
     at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
     at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
     at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.Run() 
     at System.Windows.Application.RunDispatcher(Object ignore) 
     at System.Windows.Application.RunInternal(Window window) 
     at System.Windows.Application.Run(Window window) 
     at System.Windows.Application.Run() 
     at WpfApplication1.App.Main() in D:\tmp\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs:line 0 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

我在做什麼錯?

回答

11

您正在嘗試使用GET請求,但在其中放置了一個正文。

你不能那樣做。

更改Method是「POST」:

req.Method = "POST"; 

(順便說一句,File.ReadAllBytes是從文件中讀取所有的數據更健壯,更簡單的方法。)

+2

@jiewmeng:支持喬恩的聲稱'File.ReadAllBytes'是一個更好的選擇,我會指出,當你完成它時忘記關閉FileStream。你應該把它包裝在一個使用塊中。在某些情況下,這可能不是一個合理的問題,但這是不好的做法。不過,我只是按照Jon的建議並使用'File.ReadAllBytes'。 – Brian 2010-10-08 13:55:16

相關問題