我正在尝试开发短信发送应用,但它提供了一个例外“要写入的字节数大于指定的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
}
}
我将send函数拆分为方法的原因是必须在请求之后和发送之前加载验证码。 这是终端输出:
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)
请帮忙
答案 0 :(得分:0)
您只需获取流request.GetRequestStream()
,但不写任何内容。
尝试:
var stream = request.GetRequestStream();
stream.Write(data,0,data.Length);
答案 1 :(得分:0)
还有另一种流写入方法
private void SendSMS()
{
stream.Write(dataout, 0, length);
stream.Close();
}
答案 2 :(得分:0)
您需要在 System.webServer 部分添加此部分代码,转到 Web.config 文件。
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="3000000000" />
</requestFiltering>
</security>