Hi,
Am trying to post the data over https and am getting error in
httpwebresponse .getResponseStr eam.Please help me to get rid of this
issue.
Here is the message from immediate window
?myResp.GetResp onseStream()
{System.Net.Con nectStream}
System.IO.Strea m: {System.Net.Con nectStream}
AlreadyAborted: 777777
BufferedData: <undefined value>
BufferOnly: false
BytesLeftToWrit e: 0
CallInProgress: false
CanRead: true
CanSeek: false
CanWrite: false
Connection: {System.Net.Con nection}
DataAvailable: true
drainingBuffer: {Length=1024}
Eof: false
ErrorInStream: false
IgnoreSocketWri te: false
Length: <error: an exception of type:
{System.NotSupp ortedException} occurred>
m_BufferedData: <undefined value>
m_BufferOnly: false
m_BytesLeftToWr ite: 0
m_CallNesting: 0
m_Chunked: false
m_ChunkedNeedCR LFRead: false
m_ChunkEofRecvd : false
m_ChunkSize: 0
m_ChunkTerminat or: {Length=5}
m_Connection: {System.Net.Con nection}
m_CRLF: {Length=2}
m_DoneCalled: 0
m_Draining: false
m_ErrorExceptio n: { }
m_ErrorResponse Status: false
m_IgnoreSocketW rite: false
m_MaxDrainBytes : 65536
m_NeedCallDone: true
m_ReadBuffer: {Length=4096}
m_ReadBufferSiz e: 2825
m_ReadBytes: 16436
m_ReadCallbackD elegate: {System.AsyncCa llback}
m_ReadChunkedCa llbackDelegate: {System.Threadi ng.WaitCallback }
m_ReadOffset: 1271
m_Request: <undefined value>
m_ShutDown: 0
m_TempBuffer: {Length=2}
m_Timeout: 300000
m_TotalBytesToW rite: 0
m_WriteBufferEn able: false
m_WriteCallback Delegate: {System.AsyncCa llback}
m_WriteDoneEven t: <undefined value>
m_WriteStream: false
Position: <error: an exception of type:
{System.NotSupp ortedException} occurred>
s_UnloadInProgr ess: false
StreamContentLe ngth: 16436
Timeout: 300000
TotalBytesToWri te: 0
WriteChunked: false
Below is the code:
public WebResponse MakeRequestGetR esponse(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.Cre ate(url);
//break off any domain part of the user name
string[] userNameParse = userName.Split( '\\');
string myUserName =
userNameParse[userNameParse.L ength-1];
string myDomain = userNameParse[0];
myReq.Credentia ls = new NetworkCredenti al(myUserName,
password, myDomain);
myReq.Timeout = timeoutSeconds * 1000;
myReq.CookieCon tainer = _cookieContaine r;
myReq.AllowWrit eStreamBufferin g = true;
myReq.AllowAuto Redirect = 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.Credent ials = myReq.Credentia ls;
myReq.Proxy = myProxy;
}
ServicePointMan ager.Certificat ePolicy = new
AcceptAllCertif icatePolicy();
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.GetRespon se();
case "POST":
myReq.Method = "POST";
postDataString = new StringBuilder() ;
foreach (string myKey in postData.Keys)
{
if (postDataString .Length == 0)
postDataString. Append(myKey + "=" +
(string)postDat a[myKey]);
else
postDataString. Append("&" + myKey + "=" +
(string)postDat a[myKey]);
}
ASCIIEncoding encoding = new ASCIIEncoding() ;
byte[] byteArray =
encoding.GetByt es(postDataStri ng.ToString());
myReq.ContentTy pe = "applicatio n/x-www-form-urlencoded";
myReq.ContentLe ngth = byteArray.Lengt h;
LogHelpers.LogM essage(0,"unabl e to set length");
_cookieContaine r =new CookieContainer ();
using (MemoryStream myStream = (MemoryStream)
(myReq.GetReque stStream()))
{
myStream.Write( byteArray, 0,byteArray.Len gth);
myStream.Flush( );
}
try
{
// Read the content.
HttpWebResponse myResp =
(HttpWebRespons e)myReq.GetResp onse();
// Display the status.
Debug.WriteLine (((HttpWebRespo nse)
(myResp)).Statu sDescription);
//retain the cookies
foreach (Cookie cook in myResp.Cookies)
{
_cookieContaine r.Add(cook);
}
//Check out the HTML
StreamReader sr = new
StreamReader(my Resp.GetRespons eStream());
Console.WriteLi ne(sr.ReadToEnd ());
//myWriter.Close( );
return myResp;
}
catch (Exception e)
{
throw WebRequestor.Ge tWrappedExcepti on
("Unable to open HTTP response stream.", e);
//e.Message.ToStr ing();
}
default:
throw new ReportProviderP ermanentErrorEx ception(
"Unsupporte d HTTP method: " + method);
}
}
catch (ReportProvider Exception e) {throw e;}
catch (Exception e)
{
throw new ReportProviderP ermanentErrorEx ception("Unhand led
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 NetworkCredenti al</param>
/// <param name="password" >password for NetworkCredenti al</param>
/// <param name="timeoutSe conds">timeout for the HTTP request</
param>
/// <param name="method">H TTP 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>respon se from the web server in the form of a string</
returns>
public string MakeRequestGetR esponseString(s tring url, IDictionary
postData, string userName, string password, int timeoutSeconds, string
method, string proxyUrl, bool useProxy)
{
System.Text.Str ingBuilder mySb;
using(WebRespon se myResp = MakeRequestGetR esponse(url, postData,
userName, password, timeoutSeconds, method, proxyUrl, useProxy))
{
try
{
using (Stream myRespStream = myResp.GetRespo nseStream())
{
using (StreamReader myStream = new StreamReader(my RespStream))
{
try
{
mySb = new System.Text.Str ingBuilder();
string myLine;
while ((myLine = myStream.ReadLi ne()) != null)
{
if (myLine.Length 0)
mySb.Append(myL ine);
}
}
catch (Exception e)
{
throw WebRequestor.Ge tWrappedExcepti on("Unable to read
response string.", e);
}
}
}
}
catch (Exception e)
{
throw WebRequestor.Ge tWrappedExcepti on("Unable to get response
stream.", e);
}
}
return mySb.ToString() ;
}
}
}
Also sometimes i also get this exception:
NASD.ReportProv ider.Crd.Test.C rdProviderTest. DebugInProc :
System.Applicat ionException : service returned TemporaryError on
Begin. Error messasge:
NASD.ReportProv ider.ReportProv iderTemporaryEr rorException: Unable to
get response stream. --->
NASD.ReportProv ider.ReportProv iderTemporaryEr rorException: Unable to
read response string. ---System.IO.IOExc eption: Unable to read data
from the transport connection. --->
System.Net.Sock ets.SocketExcep tion: An existing connection was
forcibly closed by the remote host
at System.Net.Sock ets.Socket.Begi nReceive(Byte[] buffer, Int32
offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback,
Object state)
at System.Net.Sock ets.NetworkStre am.BeginRead(By te[] buffer, Int32
offset, Int32 size, AsyncCallback callback, Object state)
--- End of inner exception stack trace ---
thanks,
Vishnu