你可以寫或複製您的二進制流通過獲取請求流和編寫流它來請求流。 這是某種形式的代碼,你會發現對Web請求有用的HttpWebRequest和在HttpRequest的流你的問題CAPY照片流中GetRequestStreamCallback完成()
private void UploadClick()
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("your URL of server");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
System.IO.Stream postStream = request.EndGetRequestStream(asynchronousResult);
//your binary stream to upload
BinaryStream BStream = new BinaryStream(StreamFromPhotoChooser);
//copy your data to request stream
BStream.CopyTo(postStream);
//OR
byte[] byteArray = //get bytes of choosen picture
// Write to the request stream.
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
//Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
catch (WebException e)
{
}
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
//to read server responce
Stream streamResponse = response.GetResponseStream();
string responseString = new StreamReader(streamResponse).ReadToEnd();
// Close the stream object
streamResponse.Close();
// Release the HttpWebResponse
response.Close();
Dispatcher.BeginInvoke(() =>
{
//Update UI here
});
}
}
catch (WebException e)
{
//Handle non success exception here
}
}