Perché ricevo ProtocolViolationException quando si cerca di utilizzare HttpWebRequest?

StackOverflow https://stackoverflow.com/questions/2480258

  •  21-09-2019
  •  | 
  •  

Domanda

Non ne ho idea. Sto cercando di ottenere XML da un servizio REST su un app che sto costruendo per Windows Phone. Ho sempre un'eccezione alla seguente riga:

HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;

Ho la seguente configurazione (ignorare il cattivo URL, è solo un esempio ..)

HttpWebRequest request = WebRequest.Create("https://domain.chargify.com/customers.xml") as HttpWebRequest;
NetworkCredential credentials = new NetworkCredential("appkeyhere", "password");
request.Credentials = credentials;
request.Method = "GET";
request.ContentType = "text/xml";
request.BeginGetResponse(new AsyncCallback(SomeCallback), request);
...
private void SomeCallback(IAsyncResult ar) {
    HttpWebRequest request = ar.AsyncState as HttpWebRequest;
    HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
    StreamReader reader = new StreamReader(response.GetResponseStream());
    XElement xmlResult = XElement.Parse(reader.ReadToEnd());
    ...
}

L'eccezione è la seguente:

System.Net.ProtocolViolationException was unhandled
Message=ProtocolViolationException
  StackTrace:
       at System.Net.Browser.ClientHttpWebRequest.PrepareAndSendRequest(String method, Uri requestUri, Stream requestBodyStream, WebHeaderCollection headerCollection, CookieContainer cookieContainer)
       at System.Net.Browser.ClientHttpWebRequest.BeginGetResponseImplementation()
       at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetResponse(AsyncCallback callback, Object state)
       at System.Net.Browser.AsyncHelper.BeginOnUI(BeginMethod beginMethod, AsyncCallback callback, Object state)
       at System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
       at ChargifyWPA.MainPage.button1_Click(Object sender, RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

Qualcuno ha qualche idea? E 'https, non http .. è che causa il problema? Grazie.

È stato utile?

Soluzione

La mia scommessa questo problema è dovuto al fatto che si sta configurando l'intestazione 'ContentType', ma non messa nella vostra richiesta. Si noti l'eccezione protocollo viene lanciata durante il tentativo di creare la richiesta.

Probabilmente si intende impostare la 'Accetta' intestazione a "text / xml" piuttosto che l'intestazione del tipo di contenuto.

Altri suggerimenti

Questo non è il tuo problema, ma potrebbe aiutare qualcun altro. Mi sono imbattuto in questo, perché mi stava chiamando BeginGetRequestStream () su una richiesta GET. Naturalmente, questo non è necessario, e mi avrebbe dovuto essere solo chiamate BeginGetResponse (). Stupido errore, ma avrei capito molto più veloce se il messaggio di errore era stato più utile.

"Content-Type" con "GET" causa l'eccezione se presenti in richiesta In Tabella Azure REST API è richiesto per codificare ContentType nell'intestazione auth se - utilizzare stringa vuota per dell'intestazione di GET "ContentType"

A quanto pare la causa più comune di questo errore sul desktop in questi giorni è un server web che richiede SSL, piuttosto che l'autenticazione TLS. Vedi tutti i commenti su noreferrer questo articolo che spiega le opzioni di risoluzione dei problemi per vari errori NET 2 HTTP . Sembra che il progetto Chargify.NET MIT con licenza su CodePlex è già utilizzando questo lavoro- intorno ( "SecurityProtocolType.Ssl3").

Altre opzioni da provare da un altro articolo circa 2008 sono in calo di nuovo a HTTP / 1.0 o la disabilitazione keep-alive.

Assicuratevi di provare questa risposta , rimuovendo l'inizializzazione ContentType .

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top