MSXML2.ServerXMLHTTP to HttpWebRequest: Error 500

  • Thread starter Maris Janis Vasilevskis
  • Start date
M

Maris Janis Vasilevskis

Hi,

Is it possible to force HttpWebRequest to do exactly (not approximately) the same as MSXML2.ServerXMLHTTP does?

More details. I port JScript to JScript.NET

I have a server (ASP invoking binary code; code not accessible).
I send XML requests to it.
If my request is accepted, ServerXMLHTTP returns XML with accept details.
If my request is rejected, ServerXMLHTTP returns XML with reject details.
ServerXMLHTTP always returns XML.

For accepted requests, HttpWebRequest returns the same as ServerXMLHTTP.
But for rejected requests, HttpWebRequest fails with (500) Internal Server Error.

My JScript:
function GetResponse(xmltext) {
var xmlhttp, res;
xmlhttp = new ActiveXObject("MSXML2.ServerXMLHTTP");
xmlhttp.open( "POST", MyUrl, false );
xmlhttp.setRequestHeader( "Content-Type", "text/xml" );
xmlhttp.send( xmltext );
res = xmlhttp.responseText;
xmlhttp = null;
return res;
}

My JScript.NET:
static public function GetResponse(xmltext:String):String {
var req:HttpWebRequest;
var rsp:HttpWebResponse;
var wr:StreamWriter;
var rd:StreamReader;
var res:String;
req=HttpWebRequest(WebRequest.Create(MyUrl));
req.Method='POST';
req.ContentType='text/xml';
wr=new StreamWriter(req.GetRequestStream());
try { wr.Write(xmltext); } finally { wr.Close(); }
rsp = HttpWebResponse(req.GetResponse()); // Possible Exception 500 here !!
rd=new StreamReader(rsp.GetResponseStream());
res=rd.ReadToEnd();
rd.Close();
return res;
}
I played also with Credentials, ContentLength and StreamWriter Encoding, without success.

Thank you,
Mahris
 
M

Marina

Hold on, if the request is rejected - what do you want to happen?

It doesn't sound unreasonable to have code to handle errors that may be
coming back from the server.
 
M

Maris Janis Vasilevskis

Thank you, Marina.
However, I cannot hold on, I need to analyze the reject message.

But I found the reason.
In the server trace I see that 500 is generated for both, ServerXMLHTTP and HttpWebRequest,
together with a good XML.
Simply, ServerXMLHTTP returns the result but HttpWebRequest throws error.

So, my current solution:
static public function GetResponse(xmltext:String):String {
var req:HttpWebRequest;
var rsp:HttpWebResponse;
var wr:StreamWriter;
var rd:StreamReader;
var res:String;
req=HttpWebRequest(WebRequest.Create(cst.SoapURL));
req.Method='POST';
req.ContentType='text/xml';
wr=new StreamWriter(req.GetRequestStream());
try { wr.Write(xmltext); } finally { wr.Close(); }
try {
rsp = HttpWebResponse(req.GetResponse());
}
catch(e:System.Net.WebException) {
if(e.Status==System.Net.WebExceptionStatus.ProtocolError) {
rsp = HttpWebResponse(e.Response);
}
else throw e;
}
rd=new StreamReader(rsp.GetResponseStream());
res=rd.ReadToEnd();
rd.Close();
return res;
}

Mahris
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top