[Mono-list] MONO HttpWebRequest, HttpWebResponse problems
Kieren Drapala (NetTeller)
kieren.drapala@netteller.com.au
Fri, 2 Apr 2004 16:47:24 +1000
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
------_=_NextPart_001_01C4187E.5B1E39F0
Content-Type: text/plain
Hello,
I am having HttpWebRequest, HttpWebResponse problems. Here is an example
which is similar to what I'm trying to do.
using System;
using System.IO;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Net;
namespace ConsoleApplication4
{
public class Connect
{
private const int BUFFER_SIZE= 1024;
private int TIME_OUT;
private ManualResetEvent m_eventDone;
private HttpWebRequest m_httpReq;
private byte[] m_btRead;
private StringBuilder m_sbResp;
private string m_sPost;
private Stream m_stResponse;
private Decoder m_dec = Encoding.UTF8.GetDecoder();
private string m_sResp;
private bool m_bGotResponse = false;
public Connect()
{
m_eventDone = new ManualResetEvent(false);
m_btRead = new byte[BUFFER_SIZE];
m_sbResp = new StringBuilder(String.Empty);
}
public int HUBTimeOut
{
set { TIME_OUT = value *1000;}
}
public string strResponse
{
get { return m_sResp;}
}
public bool GotResponse
{
get { return m_bGotResponse;}
}
public string postToURL(string sURL, string sData, string ProxyAddress,
string ProxyUsername, string ProxyPassword)
{
m_httpReq = (HttpWebRequest)WebRequest.Create(sURL);
/**
* If you are behind a firewall and you do not have your browser proxy
setup
* you need to use the following proxy creation code. */
if ((ProxyAddress != null) && (ProxyAddress != System.String.Empty))
{
// Create a proxy object.
WebProxy myProxy = new WebProxy();
// Associate a new Uri object to the _wProxy object, using the proxy
address
// selected by the user.
myProxy.Address = new Uri(ProxyAddress);
if (((ProxyUsername != null) && (ProxyUsername != System.String.Empty))
&& ((ProxyPassword != null) && (ProxyPassword != System.String.Empty)))
myProxy.Credentials=new NetworkCredential(ProxyUsername,
ProxyPassword);
// Finally, initialize the Web request object proxy property with the
_wProxy
// object.
m_httpReq.Proxy=myProxy;
}
m_httpReq.Method = "POST";
m_httpReq.ContentType = "application/x-www-form-urlencoded";
m_httpReq.ContentLength = sData.Length;
Stream st = m_httpReq.GetRequestStream();
StreamWriter stw = new StreamWriter(st);
stw.Write(sData);
stw.Flush();
stw.Close();
IAsyncResult ar = (IAsyncResult) m_httpReq.BeginGetResponse(
new AsyncCallback(callback), this);
//hold until we are done reading
//put a timeout here too
if ((TIME_OUT == 0))
TIME_OUT = 50000;
m_bGotResponse = m_eventDone.WaitOne(TIME_OUT,false);
m_sPost = sData;
return m_sResp;
}
public string getPostData()
{
return m_sPost;
}
private void callback(IAsyncResult ar)
{
HttpWebResponse httpResp = (HttpWebResponse)m_httpReq.EndGetResponse(ar);
// Start reading data from the response stream.
m_stResponse = httpResp.GetResponseStream();
// Pass rs.BufferRead to BeginRead. Read data into rs.BufferRead
IAsyncResult iarRead = m_stResponse .BeginRead(m_btRead, 0,
BUFFER_SIZE, new AsyncCallback(readCallback), this);
}
private void readCallback(IAsyncResult asyncResult)
{
// Read rs.BufferRead to verify that it contains data.
int iRead = m_stResponse.EndRead( asyncResult );
if (iRead > 0)
{
// Prepare a Char array buffer for converting to Unicode.
Char[] charBuffer = new Char[BUFFER_SIZE];
// Convert byte stream to Char array and then to String.
// len contains the number of characters converted to Unicode.
int iLen = m_dec.GetChars(m_btRead, 0, BUFFER_SIZE, charBuffer, 0);
String s = new String(charBuffer, 0, iLen);
// Append the recently read data to the stringbuilder
m_sbResp.Append(Encoding.ASCII.GetString(m_btRead, 0, iRead));
// Continue reading data untilm_stResponse.EndRead returns -1.
IAsyncResult ar = m_stResponse.BeginRead( m_btRead, 0, BUFFER_SIZE,
new AsyncCallback(readCallback), this);
}
else
{
if(m_btRead.Length>0)
{
//set our response string
m_sResp = m_sbResp.ToString();
}
// Close down the response stream.
m_stResponse.Close();
// Set the ManualResetEvent so the main thread can exit.
m_eventDone.Set();
}
return;
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string result1 = "";
Connect cn1 = new Connect();
cn1.HUBTimeOut = 60;
cn1.postToURL(" <https://sometestHTTPservice.com.au/OFXServlet>
https://sometestHTTPservice.com.au/OFXServlet","","http://myproxy:80/",
"","");
if (cn1.GotResponse)
{
result1 = cn1.strResponse;
Console.WriteLine(result1);
}
}
}
}
Output on windows [.NET Framework 1.1, Windows XP Pro]
<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>20007</CODE>
<SEVERITY>ERROR</SEVERITY>
<MESSAGE>DTDValidationError:XML_DOES_NOT_CONFORM_TO_DTD</MESSAGE>
</STATUS>
<DTSERVER>20040402055319</DTSERVER>
<LANGUAGE>ENG</LANGUAGE>
</SONRS>
</SIGNONMSGSRSV1>
</OFX>
Output on Debian [Tried with both Mono JIT compiler version 0.30.2 and Mono
JIT compiler version 0.29, Linux 2.4.23]
<HTML></HTML>
Please advise, as class status states these objects are finished, thanks in
advance, regards,
Kieren Drapala
Analyst / Programmer
------_=_NextPart_001_01C4187E.5B1E39F0
Content-Type: text/html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<TITLE>Message</TITLE>
<META content="MSHTML 6.00.2800.1276" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial size=2><SPAN class=016580606-02042004>Hello,
</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=016580606-02042004>I am having
HttpWebRequest, HttpWebResponse problems. Here is an example which is similar to
what I'm trying to do.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>using System;<BR>using System.IO;<BR>using
System.Diagnostics;<BR>using System.Text;<BR>using System.Threading;<BR>using
System.Net;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><BR><FONT face=Arial size=2>namespace ConsoleApplication4<BR>{</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> public class
Connect<BR> {<BR> private const int BUFFER_SIZE=
1024;<BR> private int TIME_OUT;<BR> private
ManualResetEvent m_eventDone;<BR> private HttpWebRequest
m_httpReq;<BR> private byte[] m_btRead;<BR> private
StringBuilder m_sbResp;<BR> private string
m_sPost;<BR> private Stream m_stResponse;<BR> private
Decoder m_dec = Encoding.UTF8.GetDecoder();<BR> private string
m_sResp;<BR> private bool m_bGotResponse = false;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><BR><FONT face=Arial size=2> public
Connect()<BR> {<BR> m_eventDone = new
ManualResetEvent(false);<BR> m_btRead = new
byte[BUFFER_SIZE];<BR> m_sbResp = new
StringBuilder(String.Empty);<BR> }</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> public int
HUBTimeOut<BR> {<BR> set { TIME_OUT = value
*1000;}<BR> }<BR> public string
strResponse<BR> {<BR> get { return
m_sResp;}<BR> }</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> public bool
GotResponse<BR> {<BR> get { return
m_bGotResponse;}<BR> }</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> public string postToURL(string sURL,
string sData, string ProxyAddress, <BR> string ProxyUsername,
string ProxyPassword)<BR> {<BR> m_httpReq =
(HttpWebRequest)WebRequest.Create(sURL);</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial
size=2> /**<BR> * If you are
behind a firewall and you do not have your browser proxy
setup<BR> * you need to use the following proxy
creation code. */</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> if ((ProxyAddress != null)
&& (ProxyAddress !=
System.String.Empty))<BR> {<BR> //
Create a proxy object.<BR> WebProxy myProxy = new
WebProxy();</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> // Associate a new Uri
object to the _wProxy object, using the proxy
address<BR> // selected by the
user.<BR> myProxy.Address = new
Uri(ProxyAddress);<BR> if (((ProxyUsername != null)
&& (ProxyUsername != System.String.Empty))
<BR> && ((ProxyPassword != null) &&
(ProxyPassword !=
System.String.Empty)))<BR> myProxy.Credentials=new
NetworkCredential(ProxyUsername,
ProxyPassword);<BR>
<BR> // Finally, initialize the Web request object proxy
property with the _wProxy<BR> //
object.<BR> m_httpReq.Proxy=myProxy;<BR> }</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> m_httpReq.Method =
"POST";<BR> m_httpReq.ContentType =
"application/x-www-form-urlencoded";<BR> m_httpReq.ContentLength
= sData.Length;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> Stream st =
m_httpReq.GetRequestStream();</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> StreamWriter stw = new
StreamWriter(st);<BR> stw.Write(sData);</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial
size=2> stw.Flush();<BR> stw.Close();</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> IAsyncResult ar = (IAsyncResult)
m_httpReq.BeginGetResponse(<BR> new
AsyncCallback(callback), this);</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> //hold until we are done
reading<BR> //put a timeout here too<BR> if
((TIME_OUT == 0))<BR> TIME_OUT =
50000;<BR> m_bGotResponse =
m_eventDone.WaitOne(TIME_OUT,false);<BR> m_sPost =
sData;<BR> return m_sResp;<BR> }</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> public string
getPostData()<BR> {<BR> return
m_sPost;<BR> }</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> private void callback(IAsyncResult
ar)<BR> {</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> HttpWebResponse httpResp =
(HttpWebResponse)m_httpReq.EndGetResponse(ar);
</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> // Start reading data from
the response stream.<BR> m_stResponse =
httpResp.GetResponseStream();</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT><BR><FONT face=Arial
size=2> // Pass rs.BufferRead to BeginRead. Read data
into rs.BufferRead<BR> IAsyncResult iarRead = m_stResponse
.BeginRead(m_btRead, 0, <BR> BUFFER_SIZE, new
AsyncCallback(readCallback), this); <BR> }</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial
size=2>
<BR> private void readCallback(IAsyncResult
asyncResult)<BR> {</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> // Read rs.BufferRead to verify
that it contains data. <BR> int iRead = m_stResponse.EndRead(
asyncResult );<BR> if (iRead >
0)<BR> {<BR> // Prepare a Char array
buffer for converting to Unicode.<BR> Char[] charBuffer =
new Char[BUFFER_SIZE];<BR>
<BR> // Convert byte stream to Char array and then to
String.<BR> // len contains the number of characters
converted to Unicode.<BR> int iLen =
m_dec.GetChars(m_btRead, 0, BUFFER_SIZE, charBuffer,
0);<BR> String s = new String(charBuffer, 0,
iLen);</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> // Append the recently read
data to the
stringbuilder<BR> m_sbResp.Append(Encoding.ASCII.GetString(m_btRead,
0, iRead)); </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> // Continue reading data
untilm_stResponse.EndRead returns -1.<BR> IAsyncResult ar
= m_stResponse.BeginRead( m_btRead, 0, BUFFER_SIZE,
<BR> new AsyncCallback(readCallback),
this);<BR> }<BR> else<BR> {<BR> if(m_btRead.Length>0)<BR> {<BR> //set
our response
string
<BR> m_sResp =
m_sbResp.ToString();<BR> }<BR> //
Close down the response
stream.<BR> m_stResponse.Close();
<BR> // Set the ManualResetEvent so the main thread can
exit.<BR> m_eventDone.Set();
<BR> }<BR> return;<BR> }
</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> }</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT><BR><FONT face=Arial size=2> ///
<summary><BR> /// Summary description for Class1.<BR> ///
</summary><BR> <BR> class Class1<BR> {<BR> ///
<summary><BR> /// The main entry point for the
application.<BR> ///
</summary><BR> [STAThread]<BR> static void
Main(string[] args)<BR> {<BR> string result1 =
"";<BR> Connect cn1 = new
Connect();<BR> cn1.HUBTimeOut = 60;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> cn1.postToURL("</FONT><A
href='https://sometestHTTPservice.com.au/OFXServlet","","http://myproxy:80/'><FONT
face=Arial size=2>https://<SPAN
class=016580606-02042004>sometestHTTPservice</SPAN>.com.au/OFXServlet","","http://<SPAN
class=016580606-02042004>myproxy</SPAN>:80/</FONT></A><FONT face=Arial
size=2>",<BR> "",""); <BR> if
(cn1.GotResponse)<BR> {<BR> result1 =
cn1.strResponse;<BR> Console.WriteLine(result1);<BR> }<BR> }<BR> }<BR>}<BR></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT><SPAN class=016580606-02042004><FONT face=Arial size=2>Output on
windows [.NET Framework 1.1, Windows XP Pro]</FONT></SPAN></FONT></DIV>
<DIV><FONT><SPAN class=016580606-02042004></SPAN></FONT><FONT face=Arial
size=2></FONT> </DIV><FONT><SPAN class=016580606-02042004><FONT face=Arial
size=2>
<DIV><BR><OFX><BR><SIGNONMSGSRSV1><BR><SONRS><BR><STATUS><BR><CODE>20007</CODE><BR><SEVERITY>ERROR</SEVERITY><BR><MESSAGE>DTDValidationError:XML_DOES_NOT_CONFORM_TO_DTD</MESSAGE><BR></STATUS><BR><DTSERVER>20040402055319</DTSERVER><BR><LANGUAGE>ENG</LANGUAGE><BR></SONRS><BR></SIGNONMSGSRSV1><BR></OFX></DIV>
<DIV> </DIV>
<DIV></FONT></SPAN> </DIV></FONT>
<DIV><FONT face=Arial size=2><SPAN class=016580606-02042004>Output on Debian
[Tried with both Mono JIT compiler version 0.30.2 and Mono JIT compiler version
0.29, Linux 2.4.23]</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=016580606-02042004></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN
class=016580606-02042004><HTML></HTML><BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=016580606-02042004></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=016580606-02042004>Please advise, as
class status states these objects are finished, thanks in advance, regards,
</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV><!-- Converted from text/rtf format --><SPAN
lang=en-au><FONT face=Tahoma size=2>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"
align=left><FONT face=Arial><B><SPAN lang=EN-US
style="mso-bidi-font-family: Arial; mso-ansi-language: EN-US">Kieren
Drapala</SPAN></B></FONT></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><FONT
face=Arial><B><SPAN lang=EN-US
style="mso-bidi-font-family: Arial; mso-ansi-language: EN-US">Analyst /
Programmer</SPAN></B></FONT></P></FONT></SPAN></BODY></HTML>
------_=_NextPart_001_01C4187E.5B1E39F0--