以下のコードを使用して、サイトにバイト配列を送信しています。なぜこのコードは、インターネットに接続されていない場合でも例外をスローしていないのですか?接続がない場合でもストリームを取得してit.Iに書き込むことができますが、例外をスローすることを期待しますStream postStream = request1.EndGetRequestStream(result)
。なぜそのような行動をするのか。インターネット接続がなくてもHttpWebRequestがストリームを取得しています
private void UploadHttpFile()
{
HttpWebRequest request = WebRequest.CreateHttp(new Uri(myUrl));
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.UserAgent = "Mozilla/4.0 (Windows; U; Windows Vista;)";
request.Method = "POST";
request.UseDefaultCredentials = true;
request.BeginGetRequestStream(GetStream, request);
}
private void GetStream(IAsyncResult result)
{
try
{
HttpWebRequest request1 = (HttpWebRequest)result.AsyncState;
using (Stream postStream = request1.EndGetRequestStream(result))
{
int len = postBody.Length;
len += mainBody.Length;
len += endBody.Length;
byte[] postArray = new byte[len + 1];
Encoding.UTF8.GetBytes(postBody.ToString()).CopyTo(postArray, 0);
Encoding.UTF8.GetBytes(mainBody).CopyTo(postArray, postBody.Length);
Encoding.UTF8.GetBytes(endBody).CopyTo(postArray, postBody.Length + mainBody.Length);
postStream.Write(postArray, 0, postArray.Length);
}
}
WP7では 'AllowWriteStreamBuffering'は利用できません。' AllowReadStreamBuffering'しか見ることができません。 – Vaysage
@Vaysage:ああ...その場合は、あなたが応答を得るまであなたがよく知ることができないかもしれません。あなたが書いているコードで実際に多くのことをやっているわけではないことを考えると、それは特別な苦難ですか? –
はい最後にゼロを送信する必要があります。投稿したメソッドはこれを行う最も良い方法です。このメソッドのみを使用します。 – Vaysage