Stream does not support concurrent IO read or write operations

V

vishnu

Hi,

Am trying to post the data over https and am getting error in
httpwebresponse.getResponseStream.Please help me to get rid of this
issue.

Here is the message from 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

Below is the code:

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();
}
}
}



Also sometimes i also get this exception:

NASD.ReportProvider.Crd.Test.CrdProviderTest.DebugInProc :
System.ApplicationException : service returned TemporaryError on
Begin. Error messasge:
NASD.ReportProvider.ReportProviderTemporaryErrorException: Unable to
get response stream. --->
NASD.ReportProvider.ReportProviderTemporaryErrorException: Unable to
read response string. ---> System.IO.IOException: Unable to read data
from the transport connection. --->
System.Net.Sockets.SocketException: An existing connection was
forcibly closed by the remote host
at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32
offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback,
Object state)
at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32
offset, Int32 size, AsyncCallback callback, Object state)
--- End of inner exception stack trace ---



thanks,
Vishnu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top