G
Guest
PRB - HttpWebRequest does not work with CGI websites and/or RAW data
Please help,
I'm trying to make a web deploying UserControl that gets RAW binary data
that is dynamically created from a CGI application webiste, and have the
UserControl write the data down to a file. I've used the code listed here
after to make this happen. If I plug in a URL to a non-CGI site, such as a
static HTML page, the UserControl successfully get the output from the
webbsite and writes output to file. But if I plug in a URL to my CGI app,
which returns the same HTML data (and in fact was used to create the same
aforementioned static HTML page), then I get an error on the
HttpWebRequest.GetResponseStream of "protocol violation".
I've seen many mentions of using an app.config file to "fix" this issue, but
once I changed the URL to point to a static HTML file I knew that the call to
GetResponseStream needs something to tell it to NOT analyze the output. And
being that this is a UserControl, I can not use an app.config file, as the
UserControl is dployed over the web.
Any ideas?
=============
1st of all, I expect the HttpWebRequest to NOT analyze the content of what
is being returned, and certainly NOT to throw an exception. I need for the
CGI app to get either an HTTP GET or POST with potential HTML posted data or
certainly a query string so that it can generate the dynamic data I need. For
which the CGI app then sends down the data, which potentially will be RAW
binary data.
=============== C# code...
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Reflection;
namespace MyNameSpace
{
public class MyUserControl : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
public string csKey;
public string csOptions;
public string LA;
public string ERR;
public string csTemp;
public MyUserControl()
{
InitializeComponent();
}
protected override void Dispose(bool bDisposing)
{
if (bDisposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(bDisposing);
}
#region Component Designer generated code
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
public string Run(string csKey, string csOptions)
{
try
{
LA = "Download the object";
string csTargetDirectory;
csTargetDirectory = Environment.GetFolderPath
(
System.Environment.SpecialFolder.CommonProgramFiles
);
csTargetDirectory += "\\MyProduct";
if (!System.IO.Directory.Exists(csTargetDirectory))
{
System.IO.Directory.CreateDirectory(csTargetDirectory);
}
string csTargetFile = Environment.GetFolderPath
(
System.Environment.SpecialFolder.ProgramFiles
);
csTargetFile += "\\MyProduct\\Binary-Output.dat";
FileStream FileStream_Output = new FileStream(csTargetFile,
FileMode.Create);
HttpWebRequest HttpWebRequest_Temp;
HttpWebRequest_Temp = (HttpWebRequest) WebRequest.Create
(
"http://SomeCgiWebSite/SomeCgiApp.exe"
);
HttpWebRequest_Temp.Method = "GET";
HttpWebResponse HttpWebResponse_Temp;
HttpWebResponse_Temp = (HttpWebResponse)
HttpWebRequest_Temp.GetResponse();
System.IO.Stream Stream_Input;
Stream_Input = HttpWebResponse_Temp.GetResponseStream(); // ERROR -
Protocol Violation.
int iReadLen = 0;
byte[] zbyteTemp = new byte[32000];
for
(
iReadLen = Stream_Input.Read(zbyteTemp, 0, 32000);
iReadLen > 0;
iReadLen = Stream_Input.Read(zbyteTemp, 0, 32000)
)
{
FileStream_Output.Write(zbyteTemp, 0, iReadLen);
}
Stream_Input.Close();
FileStream_Output.Close();
return "RET=OK";
}
catch(Exception Exception_ERR)
{
ERR = Exception_ERR.Message;
return "RET=ERR LA=" + LA + " ERR=" + ERR;
}
}
}
}
==================== HTML file that runs the UserControl
<html>
<body>
<object
id=MyUserControl
classid="http://MyWebSite//MyUserControl.dll#MyNameSpace.MyUserControl"
style="display: inline;"
VIEWASTEXT</object>
</body>
<script language=jscript>
var MyUserControl;
MyUserControl = document.getElementById("MyUserControl");
alert("objTest.Run = '" + MyUserControl.Run("Key", "Options") + "'...");
</script>
</html>
Please help,
I'm trying to make a web deploying UserControl that gets RAW binary data
that is dynamically created from a CGI application webiste, and have the
UserControl write the data down to a file. I've used the code listed here
after to make this happen. If I plug in a URL to a non-CGI site, such as a
static HTML page, the UserControl successfully get the output from the
webbsite and writes output to file. But if I plug in a URL to my CGI app,
which returns the same HTML data (and in fact was used to create the same
aforementioned static HTML page), then I get an error on the
HttpWebRequest.GetResponseStream of "protocol violation".
I've seen many mentions of using an app.config file to "fix" this issue, but
once I changed the URL to point to a static HTML file I knew that the call to
GetResponseStream needs something to tell it to NOT analyze the output. And
being that this is a UserControl, I can not use an app.config file, as the
UserControl is dployed over the web.
Any ideas?
=============
1st of all, I expect the HttpWebRequest to NOT analyze the content of what
is being returned, and certainly NOT to throw an exception. I need for the
CGI app to get either an HTTP GET or POST with potential HTML posted data or
certainly a query string so that it can generate the dynamic data I need. For
which the CGI app then sends down the data, which potentially will be RAW
binary data.
=============== C# code...
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Reflection;
namespace MyNameSpace
{
public class MyUserControl : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
public string csKey;
public string csOptions;
public string LA;
public string ERR;
public string csTemp;
public MyUserControl()
{
InitializeComponent();
}
protected override void Dispose(bool bDisposing)
{
if (bDisposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(bDisposing);
}
#region Component Designer generated code
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
public string Run(string csKey, string csOptions)
{
try
{
LA = "Download the object";
string csTargetDirectory;
csTargetDirectory = Environment.GetFolderPath
(
System.Environment.SpecialFolder.CommonProgramFiles
);
csTargetDirectory += "\\MyProduct";
if (!System.IO.Directory.Exists(csTargetDirectory))
{
System.IO.Directory.CreateDirectory(csTargetDirectory);
}
string csTargetFile = Environment.GetFolderPath
(
System.Environment.SpecialFolder.ProgramFiles
);
csTargetFile += "\\MyProduct\\Binary-Output.dat";
FileStream FileStream_Output = new FileStream(csTargetFile,
FileMode.Create);
HttpWebRequest HttpWebRequest_Temp;
HttpWebRequest_Temp = (HttpWebRequest) WebRequest.Create
(
"http://SomeCgiWebSite/SomeCgiApp.exe"
);
HttpWebRequest_Temp.Method = "GET";
HttpWebResponse HttpWebResponse_Temp;
HttpWebResponse_Temp = (HttpWebResponse)
HttpWebRequest_Temp.GetResponse();
System.IO.Stream Stream_Input;
Stream_Input = HttpWebResponse_Temp.GetResponseStream(); // ERROR -
Protocol Violation.
int iReadLen = 0;
byte[] zbyteTemp = new byte[32000];
for
(
iReadLen = Stream_Input.Read(zbyteTemp, 0, 32000);
iReadLen > 0;
iReadLen = Stream_Input.Read(zbyteTemp, 0, 32000)
)
{
FileStream_Output.Write(zbyteTemp, 0, iReadLen);
}
Stream_Input.Close();
FileStream_Output.Close();
return "RET=OK";
}
catch(Exception Exception_ERR)
{
ERR = Exception_ERR.Message;
return "RET=ERR LA=" + LA + " ERR=" + ERR;
}
}
}
}
==================== HTML file that runs the UserControl
<html>
<body>
<object
id=MyUserControl
classid="http://MyWebSite//MyUserControl.dll#MyNameSpace.MyUserControl"
style="display: inline;"
VIEWASTEXT</object>
</body>
<script language=jscript>
var MyUserControl;
MyUserControl = document.getElementById("MyUserControl");
alert("objTest.Run = '" + MyUserControl.Run("Key", "Options") + "'...");
</script>
</html>