本文整理汇总了C#中System.Net.WebConnectionStream类的典型用法代码示例。如果您正苦于以下问题:C# WebConnectionStream类的具体用法?C# WebConnectionStream怎么用?C# WebConnectionStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WebConnectionStream类属于System.Net命名空间,在下文中一共展示了WebConnectionStream类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadDone
static void ReadDone (IAsyncResult result)
{
WebConnection cnc = (WebConnection) result.AsyncState;
WebConnectionData data = cnc.Data;
Stream ns = cnc.nstream;
if (ns == null) {
cnc.Close (true);
return;
}
int nread = -1;
try {
nread = ns.EndRead (result);
} catch (ObjectDisposedException) {
return;
} catch (Exception e) {
if (e.InnerException is ObjectDisposedException)
return;
cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
return;
}
if (nread == 0) {
cnc.HandleError (WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
return;
}
if (nread < 0) {
cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
return;
}
int pos = -1;
nread += cnc.position;
if (cnc.readState == ReadState.None) {
Exception exc = null;
try {
pos = cnc.GetResponse (cnc.buffer, nread);
} catch (Exception e) {
exc = e;
}
if (exc != null || pos == -1) {
cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4");
return;
}
}
if (cnc.readState != ReadState.Content) {
int est = nread * 2;
int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est;
byte [] newBuffer = new byte [max];
Buffer.BlockCopy (cnc.buffer, 0, newBuffer, 0, nread);
cnc.buffer = newBuffer;
cnc.position = nread;
cnc.readState = ReadState.None;
InitRead (cnc);
return;
}
cnc.position = 0;
WebConnectionStream stream = new WebConnectionStream (cnc);
bool expect_content = ExpectContent (data.StatusCode, data.request.Method);
string tencoding = null;
if (expect_content)
tencoding = data.Headers ["Transfer-Encoding"];
cnc.chunkedRead = (tencoding != null && tencoding.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
if (!cnc.chunkedRead) {
stream.ReadBuffer = cnc.buffer;
stream.ReadBufferOffset = pos;
stream.ReadBufferSize = nread;
try {
stream.CheckResponseInBuffer ();
} catch (Exception e) {
cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone7");
}
} else if (cnc.chunkStream == null) {
try {
cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
} catch (Exception e) {
cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5");
return;
}
} else {
cnc.chunkStream.ResetBuffer ();
try {
cnc.chunkStream.Write (cnc.buffer, pos, nread);
} catch (Exception e) {
cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6");
return;
}
}
data.stream = stream;
if (!expect_content)
stream.ForceCompletion ();
//.........这里部分代码省略.........
示例2: SetWriteStream
internal void SetWriteStream (WebConnectionStream stream)
{
if (Aborted)
return;
writeStream = stream;
if (bodyBuffer != null) {
webHeaders.RemoveInternal ("Transfer-Encoding");
contentLength = bodyBufferLength;
writeStream.SendChunked = false;
}
SendRequestHeaders (false);
haveRequest = true;
if (bodyBuffer != null) {
// The body has been written and buffered. The request "user"
// won't write it again, so we must do it.
if (ntlm_auth_state != NtlmAuthState.Challenge) {
writeStream.Write (bodyBuffer, 0, bodyBufferLength);
bodyBuffer = null;
writeStream.Close ();
}
} else if (method != "HEAD" && method != "GET" && method != "MKCOL" && method != "CONNECT" &&
method != "TRACE") {
if (getResponseCalled && !writeStream.RequestWritten)
writeStream.WriteRequest ();
}
if (asyncWrite != null) {
asyncWrite.SetCompleted (false, stream);
asyncWrite.DoCallback ();
asyncWrite = null;
}
}
示例3: SetWriteStream
internal void SetWriteStream (WebConnectionStream stream)
{
if (Aborted)
return;
writeStream = stream;
if (bodyBuffer != null) {
webHeaders.RemoveInternal ("Transfer-Encoding");
contentLength = bodyBufferLength;
writeStream.SendChunked = false;
}
try {
var result = writeStream.SetHeadersAsync (false, SetWriteStreamCB, null);
if (result == null)
SetWriteStreamCB (null);
} catch (Exception exc) {
SetWriteStreamErrorCB (exc);
}
}
示例4: SetWriteStream
internal void SetWriteStream (WebConnectionStream stream)
{
if (Aborted)
return;
writeStream = stream;
if (bodyBuffer != null) {
webHeaders.RemoveInternal ("Transfer-Encoding");
contentLength = bodyBufferLength;
writeStream.SendChunked = false;
}
writeStream.SetHeadersAsync (false, result => {
if (result.GotException) {
SetWriteStreamError (result.Exception);
return;
}
haveRequest = true;
SetWriteStreamInner (inner => {
if (inner.GotException) {
SetWriteStreamError (inner.Exception);
return;
}
if (asyncWrite != null) {
asyncWrite.SetCompleted (inner.CompletedSynchronously, writeStream);
asyncWrite.DoCallback ();
asyncWrite = null;
}
});
});
}
示例5: SetWriteStream
internal void SetWriteStream (WebConnectionStream stream)
{
if (Aborted)
return;
writeStream = stream;
if (bodyBuffer != null) {
webHeaders.RemoveInternal ("Transfer-Encoding");
contentLength = bodyBufferLength;
writeStream.SendChunked = false;
}
byte[] requestHeaders = GetRequestHeaders ();
WebAsyncResult result = new WebAsyncResult (new AsyncCallback (SetWriteStreamCB), null);
writeStream.SetHeadersAsync (requestHeaders, result);
}
示例6: Abort
public override void Abort ()
{
if (Interlocked.CompareExchange (ref aborted, 1, 0) == 1)
return;
if (haveResponse && finished_reading)
return;
haveResponse = true;
if (abortHandler != null) {
try {
abortHandler (this, EventArgs.Empty);
} catch (Exception) {}
abortHandler = null;
}
if (asyncWrite != null) {
WebAsyncResult r = asyncWrite;
if (!r.IsCompleted) {
try {
WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
r.SetCompleted (false, wexc);
r.DoCallback ();
} catch {}
}
asyncWrite = null;
}
if (asyncRead != null) {
WebAsyncResult r = asyncRead;
if (!r.IsCompleted) {
try {
WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
r.SetCompleted (false, wexc);
r.DoCallback ();
} catch {}
}
asyncRead = null;
}
if (writeStream != null) {
try {
writeStream.Close ();
writeStream = null;
} catch {}
}
if (webResponse != null) {
try {
webResponse.Close ();
webResponse = null;
} catch {}
}
}
示例7: CheckFinalStatus
// Returns true if redirected
bool CheckFinalStatus (WebAsyncResult result)
{
if (result.GotException) {
bodyBuffer = null;
throw result.Exception;
}
Exception throwMe = result.Exception;
HttpWebResponse resp = result.Response;
WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
HttpStatusCode code = 0;
if (throwMe == null && webResponse != null) {
code = webResponse.StatusCode;
if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) ||
(ProxyQuery && code == HttpStatusCode.ProxyAuthenticationRequired))) {
if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
// Keep the written body, so it can be rewritten in the retry
if (InternalAllowBuffering) {
// NTLM: This is to avoid sending data in the 'challenge' request
// We save it in the first request (first 401), don't send anything
// in the challenge request and send it in the response request along
// with the buffers kept form the first request.
if (ntlm_auth_state != NtlmAuthState.Response) {
bodyBuffer = writeStream.WriteBuffer;
bodyBufferLength = writeStream.WriteBufferLength;
}
return true;
} else if (method != "PUT" && method != "POST") {
bodyBuffer = null;
return true;
}
if (!ThrowOnError)
return false;
writeStream.InternalClose ();
writeStream = null;
webResponse.Close ();
webResponse = null;
bodyBuffer = null;
throw new WebException ("This request requires buffering " +
"of data for authentication or " +
"redirection to be sucessful.");
}
}
bodyBuffer = null;
if ((int) code >= 400) {
string err = String.Format ("The remote server returned an error: ({0}) {1}.",
(int) code, webResponse.StatusDescription);
throwMe = new WebException (err, null, protoError, webResponse);
webResponse.ReadAll ();
} else if ((int) code == 304 && allowAutoRedirect) {
string err = String.Format ("The remote server returned an error: ({0}) {1}.",
(int) code, webResponse.StatusDescription);
throwMe = new WebException (err, null, protoError, webResponse);
} else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) {
throwMe = new WebException ("Max. redirections exceeded.", null,
protoError, webResponse);
webResponse.ReadAll ();
}
}
bodyBuffer = null;
if (throwMe == null) {
bool b = false;
int c = (int) code;
if (allowAutoRedirect && c >= 300) {
if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
bodyBuffer = writeStream.WriteBuffer;
bodyBufferLength = writeStream.WriteBufferLength;
}
b = Redirect (result, code);
if (b && ntlm_auth_state != 0)
ntlm_auth_state = 0;
}
if (resp != null && c >= 300 && c != 304)
resp.ReadAll ();
return b;
}
if (!ThrowOnError)
return false;
if (writeStream != null) {
writeStream.InternalClose ();
writeStream = null;
}
webResponse = null;
throw throwMe;
}
示例8: CheckFinalStatus
// Returns true if redirected
bool CheckFinalStatus (WebAsyncResult result)
{
if (result.GotException)
throw result.Exception;
Exception throwMe = result.Exception;
bodyBuffer = null;
HttpWebResponse resp = result.Response;
WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
HttpStatusCode code = 0;
if (throwMe == null && webResponse != null) {
code = webResponse.StatusCode;
if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) ||
(ProxyQuery && code == HttpStatusCode.ProxyAuthenticationRequired))) {
if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
// Keep the written body, so it can be rewritten in the retry
if (InternalAllowBuffering) {
bodyBuffer = writeStream.WriteBuffer;
bodyBufferLength = writeStream.WriteBufferLength;
return true;
} else if (method != "PUT" && method != "POST") {
return true;
}
writeStream.InternalClose ();
writeStream = null;
webResponse.Close ();
webResponse = null;
throw new WebException ("This request requires buffering " +
"of data for authentication or " +
"redirection to be sucessful.");
}
}
if ((int) code >= 400) {
string err = String.Format ("The remote server returned an error: ({0}) {1}.",
(int) code, webResponse.StatusDescription);
throwMe = new WebException (err, null, protoError, webResponse);
webResponse.ReadAll ();
} else if ((int) code == 304 && allowAutoRedirect) {
string err = String.Format ("The remote server returned an error: ({0}) {1}.",
(int) code, webResponse.StatusDescription);
throwMe = new WebException (err, null, protoError, webResponse);
} else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) {
throwMe = new WebException ("Max. redirections exceeded.", null,
protoError, webResponse);
webResponse.ReadAll ();
}
}
if (throwMe == null) {
bool b = false;
int c = (int) code;
if (allowAutoRedirect && c >= 300) {
if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
bodyBuffer = writeStream.WriteBuffer;
bodyBufferLength = writeStream.WriteBufferLength;
}
b = Redirect (result, code);
}
if (resp != null && c >= 300 && c != 304)
resp.ReadAll ();
return b;
}
if (writeStream != null) {
writeStream.InternalClose ();
writeStream = null;
}
webResponse = null;
throw throwMe;
}
示例9: CheckFinalStatus
// Returns true if redirected
bool CheckFinalStatus (WebAsyncResult result)
{
if (result.GotException) {
bodyBuffer = null;
throw result.Exception;
}
Exception throwMe = result.Exception;
HttpWebResponse resp = result.Response;
WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
HttpStatusCode code = 0;
if (throwMe == null && webResponse != null) {
code = webResponse.StatusCode;
if ((!auth_state.IsCompleted && code == HttpStatusCode.Unauthorized && credentials != null) ||
(ProxyQuery && !proxy_auth_state.IsCompleted && code == HttpStatusCode.ProxyAuthenticationRequired)) {
if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
// Keep the written body, so it can be rewritten in the retry
if (MethodWithBuffer) {
if (AllowWriteStreamBuffering) {
if (writeStream.WriteBufferLength > 0) {
bodyBuffer = writeStream.WriteBuffer;
bodyBufferLength = writeStream.WriteBufferLength;
}
return true;
}
//
// Buffering is not allowed but we have alternative way to get same content (we
// need to resent it due to NTLM Authentication).
//
if (ResendContentFactory != null) {
using (var ms = new MemoryStream ()) {
ResendContentFactory (ms);
bodyBuffer = ms.ToArray ();
bodyBufferLength = bodyBuffer.Length;
}
return true;
}
} else if (method != "PUT" && method != "POST") {
bodyBuffer = null;
return true;
}
if (!ThrowOnError)
return false;
writeStream.InternalClose ();
writeStream = null;
webResponse.Close ();
webResponse = null;
bodyBuffer = null;
throw new WebException ("This request requires buffering " +
"of data for authentication or " +
"redirection to be sucessful.");
}
}
bodyBuffer = null;
if ((int) code >= 400) {
string err = String.Format ("The remote server returned an error: ({0}) {1}.",
(int) code, webResponse.StatusDescription);
throwMe = new WebException (err, null, protoError, webResponse);
webResponse.ReadAll ();
} else if ((int) code == 304 && allowAutoRedirect) {
string err = String.Format ("The remote server returned an error: ({0}) {1}.",
(int) code, webResponse.StatusDescription);
throwMe = new WebException (err, null, protoError, webResponse);
} else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) {
throwMe = new WebException ("Max. redirections exceeded.", null,
protoError, webResponse);
webResponse.ReadAll ();
}
}
bodyBuffer = null;
if (throwMe == null) {
bool b = false;
int c = (int) code;
if (allowAutoRedirect && c >= 300) {
b = Redirect (result, code, webResponse);
if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
bodyBuffer = writeStream.WriteBuffer;
bodyBufferLength = writeStream.WriteBufferLength;
}
if (b && !unsafe_auth_blah) {
auth_state.Reset ();
proxy_auth_state.Reset ();
}
}
if (resp != null && c >= 300 && c != 304)
resp.ReadAll ();
return b;
}
//.........这里部分代码省略.........