WebRequest -="The request was aborted: The request was canceled."
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: WebRequest -="The request was aborted: The request was canceled."

  1. #1
    Join Date
    May 2004
    Location
    Israel
    Posts
    174

    WebRequest -="The request was aborted: The request was canceled."

    when i send an xml to a server using WebRequest object (i am sending a paramater+xml in size of about 250 chars)

    i recve an error :

    System.Net.WebException was unhandled
    Message="The request was aborted: The request was canceled."

    and the inner exception is :{"Cannot close stream until all bytes are written."}

    this happens row 63 ==>>SW.Close()


    as i understand this , the request to the server didnt finish the sending and its being closed!

    how can i prevent this?

    Code:
    1    Public Shared Function Send(ByVal URL As String, _
    2    
    3    Optional ByVal PostData As String = "", _
    4    
    5    Optional ByVal Method As HTTPMethod = HTTPMethod.HTTP_GET, _
    6    
    7    Optional ByVal ContentType As String = "")
    8    
    9    Dim Request As HttpWebRequest = WebRequest.Create(URL)
    10   
    11   Dim Response As HttpWebResponse
    12   
    13   Dim SW As StreamWriter
    14   
    15   Dim SR As StreamReader
    16   
    17   Dim ResponseData As String
    18   
    19   ' Prepare Request Object
    20   
    21   Request.Method = Method.ToString().Substring(5)
    22   
    23   ' Set form/post content-type if necessary
    24   
    25   If (Method = HTTPMethod.HTTP_POST AndAlso PostData <> "" AndAlso ContentType = "") Then
    26   
    27   ContentType = "application/x-www-form-urlencoded"
    28   
    29   End If
    30   
    31   ' Set Content-Type
    32   
    33   If (ContentType <> "") Then
    34   
    35   Request.ContentType = ContentType
    36   
    37   Request.ContentLength = PostData.Length
    38   
    39   End If
    40   
    41   'Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(PostData)
    42   
    43   ' Send Request, If Request
    44   
    45   If (Method = HTTPMethod.HTTP_POST) Then
    46   
    47   Try
    48   
    49   SW = New StreamWriter(Request.GetRequestStream())
    50   
    51   SW.Write("XML=" & RepalceData(PostData))
    52   
    53   Catch Ex As Exception
    54   
    55   Throw Ex
    56   
    57   MsgBox(Ex.Message)
    58   
    59   '
    60   
    61   Finally
    62   
    63   SW.Close()
    64   
    65   End Try
    66   
    67   End If
    68   
    69   ' Receive Response
    70   
    71   Try
    72   
    73   Response = Request.GetResponse()
    74   
    75   SR = New StreamReader(Response.GetResponseStream())
    76   
    77   ResponseData = SR.ReadToEnd()
    78   
    79   MsgBox(ResponseData)
    80   
    81   Catch Wex As System.Net.WebException
    82   
    83   SR = New StreamReader(Wex.Response.GetResponseStream())
    84   
    85   ResponseData = SR.ReadToEnd()
    86   
    87   Throw New Exception(ResponseData)
    88   
    89   Finally
    90   
    91   SR.Close()
    92   
    93   End Try
    94   
    95   Return ResponseData
    96   
    97   End Function
    98   
    99   
    100  Public Shared Function RepalceData(ByRef data)
    101  
    102  data = Replace(data, "%", "%25")
    103  
    104  data = Replace(data, " ", "%20")
    105  
    106  data = Replace(data, "#", "%23")
    107  
    108  data = Replace(data, "&", "%26")
    109  
    110  data = Replace(data, "?", "%3F")
    111  
    112  data = Replace(data, "+", "%2B")
    113  
    114  RepalceData = data
    115  
    116  End Function
    117
    thnaks i nadvance

    peleg

  2. #2
    Join Date
    Dec 2009
    Posts
    1

    Re: WebRequest -="The request was aborted: The request was canceled."

    Have you tried 'SW.Flush()' before the 'SW.Close()' call?

  3. #3
    Join Date
    Mar 2010
    Posts
    1

    Re: WebRequest -="The request was aborted: The request was canceled."

    The cancellation is originating from your SOAP client. Usually from the length of the encoded packet not equally that actual encoded length of the packet. The error is a bit of a red-herring.

    You need to set the lenth of of the pack request, to the encoded length. You'll find you only get the error when you try to send extended characters (> ascii 128)

    I blogged about it here.

    http://01792.org/blog/post/2010/03/1...ebRequest.aspx

  4. #4
    Join Date
    May 2004
    Location
    Israel
    Posts
    174

    Re: WebRequest -="The request was aborted: The request was canceled."

    thnaks both of you
    the 'SW.Flush()' was the reason,
    and i added the comment of miloTheGreat .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center