2015-07-08 14 views
9

Sto ottenendo il "System.Net.ProtocolViolationExceptionSystem.Net.ProtocolViolationException: è necessario scrivere ContentLength byte al flusso di richiesta prima di chiamare [Iniziare] GetResponse

: È necessario scrivere ContentLength byte al flusso di richiesta prima di chiamare [Begin] GetResponse "errore durante la chiamata al metodo" BeginGetResponse "della richiesta web.

Questo è il mio codice:

try 
{ 
    Stream dataStream = null; 
    WebRequest Webrequest; 
    Webrequest = WebRequest.Create(this.EndPointAddress); 
    Webrequest.Credentials = new NetworkCredential(this.username, this.password); 

    Webrequest.ContentType = "text/xml"; 
    Webrequest.Method = WebRequestMethods.Http.Post;      

    byteArray = System.Text.Encoding.UTF8.GetBytes(xmlRequest.Children[0].InnerXML); 

    Webrequest.ContentLength = byteArray.Length; 

    dataStream = Webrequest.GetRequestStream(); 
    dataStream.Write(byteArray, 0, byteArray.Length);     

    RequestState rs = new RequestState(); 
    rs.Request = Webrequest;      

    IAsyncResult r = (IAsyncResult)Webrequest.BeginGetResponse(new AsyncCallback(RespCallback), rs); 
} 
catch (Exception exc) 
{      
    TRACE.EXCEPTION(exc); 
} 
finally 
{ 
    dataStream.Close(); 
} 

In particolare, dopo aver chiamato alla funzione "getRequestStream()", il flusso sta gettando questa eccezione per la lunghezza:

'flusso .Length 'ha gettato un'eccezione di tipo' System.NotSupportedException '

Che cosa potrebbe causare?

+0

Si prega di non aggiungere "alot cercato" e "grazie" al tuo post. Per mostrare lo sforzo - inserisci il codice (ok come è nel post corrente) e link alle soluzioni che hai provato con una frase breve che non ha risolto il tuo problema. –

+0

possibile duplicato di [System.Net.ProtocolViolationException Eccezione C# Post e Getresponse] (http://stackoverflow.com/questions/18192423/system-net-protocolviolationexception-exception-c-sharp-post-and-getresponse) –

+0

Quella domanda spiega un errore diverso da questo, quindi non è duplicato. – user3815821

risposta

10

Questo alla fine ha funzionato utilizzando:

using (dataStream = Webrequest.GetRequestStream()) 
{ 
    dataStream.Write(byteArray, 0, byteArray.Length); 
} 

Invece di:

dataStream = Webrequest.GetRequestStream(); 
dataStream.Write(byteArray, 0, byteArray.Length); 
+1

perché in quest'ultimo caso il flusso non viene scaricato correttamente, credo. –

3

Il codice dovrebbe funzionare per NET 2.0 Da 4.0 e fino si dovrebbe chiudere il flusso Dopo aver scritto:

dataStream = Webrequest.GetRequestStream(); 
dataStream.Write(byteArray, 0, byteArray.Length); 
datastream.Close(); 
0

Controllare per verificare che il server sia configurato per accettare file di grandi dimensioni. Potresti scoprire che stai utilizzando il limite predefinito di 4 megabyte.

Aggiungere il seguente al file web.config per file di grandi dimensioni upload:

<system.webServer> 
    <security> 
     <requestFiltering> 
      <requestLimits maxAllowedContentLength="104857600" /> 
     </requestFiltering> 
    </security> 
</system.webServer> 
Problemi correlati