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;
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;
{
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;}
}
{
set { TIME_OUT = value *1000;}
}
public string strResponse
{
get { return m_sResp;}
}
public bool
GotResponse
{
get { return m_bGotResponse;}
}
{
get { return m_bGotResponse;}
}
public string postToURL(string sURL,
string sData, string ProxyAddress,
string ProxyUsername, string ProxyPassword)
{
m_httpReq = (HttpWebRequest)WebRequest.Create(sURL);
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 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();
{
// 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;
}
// 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;
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.Write(sData);
stw.Flush();
stw.Close();
stw.Close();
IAsyncResult ar = (IAsyncResult)
m_httpReq.BeginGetResponse(
new AsyncCallback(callback), this);
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;
}
//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;
}
{
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();
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);
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));
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;
}
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","","http://myproxy:80/",
"","");
if (cn1.GotResponse)
{
result1 = cn1.strResponse;
Console.WriteLine(result1);
}
}
}
}
"","");
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
