0

我正在尝试开发短信发送应用程序,但它给出了一个异常“要写入的字节数大于指定的 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)

请帮忙

4

3 回答 3

0

你只是得到流request.GetRequestStream(),但什么也不写。

尝试:

var stream = request.GetRequestStream();
stream.Write(data,0,data.Length);
于 2013-04-15T09:51:08.823 回答
0

还有另一种流写方法

private void SendSMS()
{
    stream.Write(dataout, 0, length);
    stream.Close();
}
于 2013-04-15T10:17:29.727 回答
0

您需要通过在System.webServer部分添加这部分代码来转到Web.config文件。

<security> 
<requestFiltering> 
<requestLimits maxAllowedContentLength="3000000000" /> 
</requestFiltering> 
</security>
于 2017-02-10T07:27:22.307 回答