Stream does not support concurrent IO read or write operations

538 views
Skip to first unread message

vishnu

unread,
Dec 14, 2007, 3:18:58 PM12/14/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,

Am trying to post data stream over http and am getting "Stream does
not support concurrent IO read or write operations".Hee is the code
below:


public WebResponse MakeRequestGetResponse(string url, IDictionary
postData, string userName, string password, int timeoutSeconds, string
method, string proxyUrl, bool useProxy)
{
try
{
#region set up HttpWebRequest and associated objects
HttpWebRequest myReq;
myReq = (HttpWebRequest)WebRequest.Create(url);

//break off any domain part of the user name
string[] userNameParse = userName.Split('\\');
string myUserName =
userNameParse[userNameParse.Length-1];
string myDomain = userNameParse[0];

myReq.Credentials = new NetworkCredential(myUserName,
password, myDomain);
myReq.Timeout = timeoutSeconds * 1000;
myReq.CookieContainer = _cookieContainer;
myReq.AllowWriteStreamBuffering = true;
myReq.AllowAutoRedirect = true;


// Set Proxy information useProxy is false for all
non DEV environment
// Currently we only need proxy for DEV
if (useProxy)
{
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri(proxyUrl);
myProxy.Address = newUri;
myProxy.Credentials = myReq.Credentials;
myReq.Proxy = myProxy;
}

ServicePointManager.CertificatePolicy = new
AcceptAllCertificatePolicy();
StringBuilder postDataString = null;
#endregion

#region stream POST or GET to HTTP server and synchronously
open and return the response stream



switch (method)
{
case "HEAD":
case "GET":

myReq.Method = method;
return myReq.GetResponse();

case "POST":
myReq.Method = "POST";
postDataString = new StringBuilder();
foreach (string myKey in postData.Keys)
{
if (postDataString.Length == 0)
postDataString.Append(myKey + "=" +
(string)postData[myKey]);
else
postDataString.Append("&" + myKey + "=" +
(string)postData[myKey]);
}

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byteArray =
encoding.GetBytes(postDataString.ToString());
myReq.ContentType = "application/x-www-form-urlencoded";
myReq.ContentLength = byteArray.Length;
LogHelpers.LogMessage(0,"unable to set length");
_cookieContainer =new CookieContainer();


using (MemoryStream myStream = (MemoryStream)
(myReq.GetRequestStream()))
{
myStream.Write(byteArray, 0,byteArray.Length);
myStream.Flush();
}
try
{

// Read the content.

HttpWebResponse myResp =
(HttpWebResponse)myReq.GetResponse();
// Display the status.
Debug.WriteLine(((HttpWebResponse)
(myResp)).StatusDescription);


//retain the cookies
foreach (Cookie cook in myResp.Cookies)
{
_cookieContainer.Add(cook);
}
//Check out the HTML
StreamReader sr = new
StreamReader(myResp.GetResponseStream());
Console.WriteLine(sr.ReadToEnd());

//myWriter.Close();
return myResp;


}


catch (Exception e)
{
throw WebRequestor.GetWrappedException
("Unable to open HTTP response stream.", e);
//e.Message.ToString();
}



default:

throw new ReportProviderPermanentErrorException(
"Unsupported HTTP method: " + method);
}
}

catch (ReportProviderException e) {throw e;}
catch (Exception e)
{
throw new ReportProviderPermanentErrorException("Unhandled
error.", e);
}
//return myStream;
#endregion
}

/// <summary>
/// Makes an HTTP request to the URL, passing post parameters as
defined by postData,
/// returning a String response.
/// </summary>
/// <param name="url">URL of the request</param>
/// <param name="postData">string of parameters to post by name
and value. If null,
/// then the request is a GET request; otherwise it is a POST
request.</param>
/// <param name="userName">username for NetworkCredential</param>
/// <param name="password">password for NetworkCredential</param>
/// <param name="timeoutSeconds">timeout for the HTTP request</
param>
/// <param name="method">HTTP method. Must be "GET", "HEAD" or
"POST"</param>
/// <param name="proxyUrl">Url for proxy server if one is needed
or empty string</param>
/// <param name="useProxy">True if you need to use proxy server </
param>
/// <returns>response from the web server in the form of a string</
returns>
public string MakeRequestGetResponseString(string url, IDictionary
postData, string userName, string password, int timeoutSeconds, string
method, string proxyUrl, bool useProxy)

{
System.Text.StringBuilder mySb;

using(WebResponse myResp = MakeRequestGetResponse(url, postData,
userName, password, timeoutSeconds,method, proxyUrl, useProxy))

{
try
{
using (Stream myRespStream = myResp.GetResponseStream())
{
using (StreamReader myStream = new StreamReader(myRespStream))
{
try
{
mySb = new System.Text.StringBuilder();
string myLine;

while ((myLine = myStream.ReadLine()) != null)
{
if (myLine.Length > 0)
mySb.Append(myLine);
}
}

catch (Exception e)
{
throw WebRequestor.GetWrappedException("Unable to read
response string.", e);
}
}
}
}
catch (Exception e)
{
throw WebRequestor.GetWrappedException("Unable to get response
stream.", e);
}
}

return mySb.ToString();
}
}
}

When i debug the code, am getting this error in
httpresponse.getReesponseStream() and i saw this message on immediate
window :

?myResp.GetResponseStream()
{System.Net.ConnectStream}
System.IO.Stream: {System.Net.ConnectStream}
AlreadyAborted: 777777
BufferedData: <undefined value>
BufferOnly: false
BytesLeftToWrite: 0
CallInProgress: false
CanRead: true
CanSeek: false
CanWrite: false
Connection: {System.Net.Connection}
DataAvailable: true
drainingBuffer: {Length=1024}
Eof: false
ErrorInStream: false
IgnoreSocketWrite: false
Length: <error: an exception of type:
{System.NotSupportedException} occurred>
m_BufferedData: <undefined value>
m_BufferOnly: false
m_BytesLeftToWrite: 0
m_CallNesting: 0
m_Chunked: false
m_ChunkedNeedCRLFRead: false
m_ChunkEofRecvd: false
m_ChunkSize: 0
m_ChunkTerminator: {Length=5}
m_Connection: {System.Net.Connection}
m_CRLF: {Length=2}
m_DoneCalled: 0
m_Draining: false
m_ErrorException: { }
m_ErrorResponseStatus: false
m_IgnoreSocketWrite: false
m_MaxDrainBytes: 65536
m_NeedCallDone: true
m_ReadBuffer: {Length=4096}
m_ReadBufferSize: 2825
m_ReadBytes: 16436
m_ReadCallbackDelegate: {System.AsyncCallback}
m_ReadChunkedCallbackDelegate: {System.Threading.WaitCallback}
m_ReadOffset: 1271
m_Request: <undefined value>
m_ShutDown: 0
m_TempBuffer: {Length=2}
m_Timeout: 300000
m_TotalBytesToWrite: 0
m_WriteBufferEnable: false
m_WriteCallbackDelegate: {System.AsyncCallback}
m_WriteDoneEvent: <undefined value>
m_WriteStream: false
Position: <error: an exception of type:
{System.NotSupportedException} occurred>
s_UnloadInProgress: false
StreamContentLength: 16436
Timeout: 300000
TotalBytesToWrite: 0
WriteChunked: false

Please let me know how can i solve this issue.

Thanks,
Vishnu

vishnu

unread,
Dec 14, 2007, 3:18:58 PM12/14/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting

Andrew Badera

unread,
Dec 15, 2007, 4:15:41 AM12/15/07
to DotNetDe...@googlegroups.com
That's a lot of code. Please try pasting it on http://pastebin.com/ and linking us to your pasted code.

Thanks-
--Andrew Badera



                                                                        while ((myLine = myStream.ReadLine ()) != null)

Peter Groenewegen

unread,
Dec 15, 2007, 4:19:14 AM12/15/07
to DotNetDe...@googlegroups.com
You are cross posting the same problem, why are you not giving more info in your earlier thread?
 
 
--
Peter

Michael O'Neill

unread,
Dec 15, 2007, 8:01:51 PM12/15/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Your post is simply too lengthy (and posted it twice?). Without
reviewing it my guess is that you are trying to do two things on one
stream and you generally can't do that.

Pare down and pinpoint your code to only the most relevant sections,
and yes by all means give pastebin.com a whirl. I'm sure more folks
will take a look at your code then.

Michael
http://crisatunity.com
> ...
>
> read more >>

vishnu

unread,
Dec 17, 2007, 11:28:19 AM12/17/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Micheal,

Here is the link for the code: http://pastebin.com/m3553e13a
Please let me know why am getting this "System.NotSupported: Stream
does not support concurrent IO read or write operations.

Thanks,
Vishnu

On Dec 15, 8:01 pm, "Michael O'Neill" <cleveridea....@gmail.com>
wrote:
> Your post is simply too lengthy (and posted it twice?). Without
> reviewing it my guess is that you are trying to do two things on one
> stream and you generally can't do that.
>
> Pare down and pinpoint your code to only the most relevant sections,
> and yes by all means give pastebin.com a whirl. I'm sure more folks
> will take a look at your code then.
>
> Michaelhttp://crisatunity.com
> ...
>
> read more >>- Hide quoted text -
>
> - Show quoted text -

Andrew Badera

unread,
Dec 18, 2007, 4:53:20 AM12/18/07
to DotNetDe...@googlegroups.com
Vishnu-

Thank you very much for using pastebin.

As Michael pointed out, it would be quite helpful if we knew exactly where the error was occurring.

Pastebin uses "@@" to highlight lines. If you could highlight the line on which your exception is thrown, I think we'd be able to help you further.

Thanks-
--Andrew

Michael O

unread,
Dec 18, 2007, 10:37:17 AM12/18/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Still too much code. Why won't you eliminate all the code that
works? Plus, you've pasted in huge 20-space indents and newlines,
which means the code is hard to read on pastebin and requires editing
when bringing into visual studio.

Given your difficulties in expressing a more exact coding problem, I
suspect going through the exercise of pinpointing your code that is
problematic will probably be all you need to solve you own problem.

Michael O
http://blog.crisatunity.com

On Dec 17, 10:28 am, vishnu <set...@gmail.com> wrote:
> Hi Micheal,
>
> Here is the link for the code:http://pastebin.com/m3553e13a
> Please let me know why am getting this "System.NotSupported: Stream
> does not support concurrent IO read or write operations.
>
> Thanks,
> Vishnu
d more >>

Alexander Higgins

unread,
Dec 19, 2007, 9:56:46 PM12/19/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
using(WebResponse myResp = MakeRequestGetResponse(url, postData,
userName, password,
timeoutSeconds,method, proxyUrl, useProxy))
{
Using (Stream myRespStream = myResp.GetResponseStream())
{
Error --> using (StreamReader myStream = new
StreamReader(myRespStream))


=================================================

Why? Becuase in myResp.GetResponseStream()) is trying to open a
stream which is currently being accessed by the MakeRequestGetResponse
function

........
StreamReader sr = new
StreamReader(myResp.GetResponseStream());
Console.WriteLine(sr.ReadToEnd());
\\ Response is returned but the sr StreamReader object has
not been closed
return myResp;

=================================================
Reply all
Reply to author
Forward
0 new messages