SMS送信アプリを開発しようとしていますが、「書き込まれるバイト数が指定されたContentLengthを超えています」という例外が発生します。コードは次のとおりです
using System;
using Gtk;
using WebKit;
using System.Text;
using System.Net;
using System.IO;
public partial class MainWindow: Gtk.Window
{
byte[] dataout; //global variables
Stream stream = null;
int length;
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
requestweb ();
this.DeleteEvent += OnDeleteEvent;
}
private void requestweb() //the post method
{
System.Net.ServicePointManager.ServerCertificateValidationCallback += (s,ce,ca,p) => true;
string index = entryIndex.Text;
string number = entryNumber.Text;
string sms = textviewSMS.Buffer.Text;
string captcha = entryCaptcha.Text;
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "send=1&smstext=" + sms + "&smstoprefix=" + index + "&smsto=" + number + "&dirtysmstext=" + sms
+ "&translit=on&confirm_key=1&confirmcode=" + captcha + "&x=0&y=0";
byte[] data = encoding.GetBytes (postData);
WebRequest request = WebRequest.Create("https://mobile.beeline.ge/ge/main/sms/send.wbp");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
stream = request.GetRequestStream();
dataout = data;
length = data.Length;
}
private void addWebView() //load captcha url
{
WebView web = new WebView ();
web.Open ("https://mobile.beeline.ge/ge/main/sms/mamimg.aspx");
web.SetSizeRequest(60,19);
captchaImage.Add (web);
captchaImage.ShowAll ();
}
protected void OnButtonSendClicked (object sender, EventArgs e)
{
SendSMS ();
}
private void SendSMS()
{
stream.Write(dataout, 0, length);
stream.Close();
}
void OnDeleteEvent(object o, DeleteEventArgs args){
Application.Quit();
args.RetVal = true; //this is necessary to stop an error on close.
}
protected void OnButton1Clicked (object sender, EventArgs e)
{
addWebView (); //this loads captcha
}
}
送信関数をメソッドに分割した理由は、リクエスト後、送信前にキャプチャをロードする必要があるためです。端末出力は次のとおりです。
alex@alex-PC:~$ mono /home/alex/C#/BeeSMS/BeeSMS/bin/Release/BeeSMS.exe
No bp log location saved, using default.
[000:000] Browser XEmbed support present: 1
[000:000] Browser toolkit is Gtk2.
[000:000] Using Gtk2 toolkit
No bp log location saved, using default.
[000:013] Warning(optionsfile.cc:47): Load: Could not open file, err=2
[000:013] No bp log location saved, using default.
[000:013] Browser XEmbed support present: 1
[000:013] Browser toolkit is Gtk2.
[000:013] Using Gtk2 toolkit
[000:000] Warning(optionsfile.cc:47): Load: Could not open file, err=2
[000:000] No bp log location saved, using default.
java version "1.7.0_15"
OpenJDK Runtime Environment (IcedTea7 2.3.7) (7u15-2.3.7-0ubuntu1~12.10.1)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
Marshaling clicked signal
Exception in Gtk# callback delegate
Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.ProtocolViolationException: The number of bytes to be written is greater than the specified ContentLength.
at System.Net.WebConnectionStream.CheckWriteOverflow (Int64 contentLength, Int64 totalWritten, Int64 size) [0x00000] in <filename unknown>:0
at System.Net.WebConnectionStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0
at System.Net.WebConnectionStream.Write (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
at MainWindow.SendSMS () [0x00000] in <filename unknown>:0
at MainWindow.OnButtonSendClicked (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <filename unknown>:0
at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x00000] in <filename unknown>:0
at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00000] in <filename unknown>:0
at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in <filename unknown>:0
at GLib.Signal.ClosureInvokedCB (System.Object o, GLib.ClosureInvokedArgs args) [0x00000] in <filename unknown>:0
at GLib.SignalClosure.Invoke (GLib.ClosureInvokedArgs args) [0x00000] in <filename unknown>:0
at GLib.SignalClosure.MarshalCallback (IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data) [0x00000] in <filename unknown>:0
at GLib.ExceptionManager.RaiseUnhandledException(System.Exception e, Boolean is_terminal)
at GLib.SignalClosure.MarshalCallback(IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data)
at Gtk.Application.gtk_main()
at Gtk.Application.Run()
at BeeSMS.MainClass.Main(System.String[] args)
助けてください