Showing posts with label consume. Show all posts
Showing posts with label consume. Show all posts

Saturday, March 24, 2012

System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

Hi
I am trying to consume a web service from asp.net web page through
HTTP POST method and I am getting the following error message

"System.Net.WebException: The remote server returned an error: (500)
Internal Server Error."

I have been searchign the web and followed all the options which worked

for others

1. Changed the web.config of the web service to accomodate HttpPost
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices
2. Used the HttpRequest.Credentials =
CredentialCache.DefaultCredentials;

I am not sure what is wrong there it fails while the request is sent to

the web service. The web service works fine with other web clients

Let me know your ideas

thanks
MohanOther web clients? What is different? Run the debugger on the server,
set a breakpoint and POST from your client, and step through the code
sirfunusa wrote:
> Other web clients? What is different? Run the debugger on the server,
> set a breakpoint and POST from your client, and step through the code

Other clients I mean when I reference the web service and access the
web method it works fine...But when I use an ASP.NET page with the
HTTPReuest Object as shown below it throws the error

"System.Net.WebException: The remote server returned an error: (500)
Internal Server Error."

Code :

HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://localhost//SubtractNumbers//Service1.asmx//Subtract");

wr.Method = "POST";
wr.ContentType = "application/x-www-form-urlencoded";
//wr.ContentLength = bytes.Length;

StreamWriter writer = new
StreamWriter(wr.GetRequestStream(),System.Text.Enc oding.UTF8);
//// Write the xml text into the stream
writer.WriteLine("Hi");
writer.Close();

WebResponse wrs = wr.GetResponse();
Stream strm = wrs.GetResponseStream();
StreamReader sr = new StreamReader(strm);
String line;
while( (line = sr.ReadLine()) != null)
Response.Write(line);
strm.Close();
sirfunusa wrote:
> Other web clients? What is different? Run the debugger on the server,
> set a breakpoint and POST from your client, and step through the code

Other clients I mean when I reference the web service and access the
web method it works fine...But when I use an ASP.NET page with the
HTTPReuest Object as shown below it throws the error

"System.Net.WebException: The remote server returned an error: (500)
Internal Server Error."

Code :

HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://localhost//SubtractNumbers//Service1.asmx//Subtract");

wr.Method = "POST";
wr.ContentType = "application/x-www-form-urlencoded";
//wr.ContentLength = bytes.Length;

StreamWriter writer = new
StreamWriter(wr.GetRequestStream(),System.Text.Enc oding.UTF8);
//// Write the xml text into the stream
writer.WriteLine("Hi");
writer.Close();

WebResponse wrs = wr.GetResponse();
Stream strm = wrs.GetResponseStream();
StreamReader sr = new StreamReader(strm);
String line;
while( (line = sr.ReadLine()) != null)
Response.Write(line);
strm.Close();
Are you sending the proper XML message (SOAP)? If your message isn't
formatted properly, you will definately get a server 500 error back.
It looks liek you are actually just sending text ("Hi"). That's not a
proper SOAP message. Here is a sample of a SOAP message:
http://www.w3.org/2004/06/03-google-soap-wsdl.html .

I personally much of web services manually because it's just easier to
interop with COM that way.

Here's part of what I do... (also be sure to set your SOAPAction in the
http header!) messageData hold my XML message I'm sending.

ASCIIEncoding encoding = new ASCIIEncoding( );
byte[] buffer = encoding.GetBytes(messageData);

HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create(this.EndPoint);
myRequest.Method = "POST";
myRequest.ContentType = "text/xml";
myRequest.ContentLength = buffer.Length;
myRequest.Headers.Add(String.Format("SOAPAction: \"{0}\"",
operationName));
myRequest.Timeout = 10000;

using (Stream newStream = myRequest.GetRequestStream( )) {
newStream.Write(buffer, 0, buffer.Length);
newStream.Close( );
}

HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myRequest.GetResponse( );

string rawResponse =
GetWebResponseString(myHttpWebResponse);

myHttpWebResponse.Close( );

Here is the GetWebResponseString method...

private static string GetWebResponseString(WebResponse
myHttpWebResponse) {
StringBuilder rawResponse = new StringBuilder( );
using (Stream streamResponse =
myHttpWebResponse.GetResponseStream( )) {
using (StreamReader streamRead = new
StreamReader(streamResponse)) {
Char[] readBuffer = new Char[256];
int count = streamRead.Read(readBuffer, 0, 256);

while (count > 0) {
String resultData = new String(readBuffer, 0,
count);
rawResponse.Append(resultData);
count = streamRead.Read(readBuffer, 0, 256);
}
}
}
return rawResponse.ToString( );
}

Look closely at what is returned. You may actually be getting the
reason for the fault in the message.

Also, if all is successful, you will get an XML message back. Are you
prepared to parse it?

You may want to actually use the .NET web service mechanism if you
don't want to do everything manually. Since you aren't using the .NET
web service mechanism the web.config configuration doesn't apply.

If you are intending to use .NET for web services, then all you have to
do is add the WSDL file as a web reference and use it as though it were
local. There's not really much work to it. If that is the case then
everything I said above is void, since this is the manual way to do it.

System.Net.WebException: The underlying connection was closed:

Hi Tim,

From your description, you've a ASP.NET webservcie and created a Winform
client app to consume it. However, when calling the webservice, you found
it worked well the first time but occured unexpected error the second time
after a few minutes, yes?

From the code you provided(calling the webservice), it seems all right and
I don't think the problem is likely in the client application. I'm not sure
about the webservice's detailed code logic in its webmethods but from the
method's signature:

WebSvcW.OMTestAddOrderXML(comboBox3.Text,int.Parse (textBox2.Text),comboBox4.
Text);

it takes simply type params, so I think we can test the webservice through
IE, open the certain url (http://...... .asmx) in the IE and call the
webservice thourgh it. It is better if you can use VS.NET to debug the
webservice on the serverside to see whether the problem also occured when
calling it from IE. If still remains, we can confirm that the problem is
not caused by the winform client , do you think so?

In addition, as for creating client application to consume webservice,
below is the related web link in MSDN:
#Creating Clients for XML Web Services
http://msdn.microsoft.com/library/e...atingclientsfor
webservices.asp?frame=true

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspxHi Tim,

Have you had a chance to view the suggestions in my last reply or have you
got any progresses on this issue? If there is anything else I can help,
please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Thanks for your reply. However, when I try to go directly to the ..asmx url via IA, when I click on the name of the web service in order to test, I receive this message - Tes
The test form is only available for requests from the local machine. Therefore, I am unclear on how to proceed. The server is in another state so I can't logon directly to it in order to test from my location. How shall I proceed

Thank you
Tim Reynold
Verizon
Hi Tim,

From your reply, you haven't the certain permission to logon the server
which host the webservice, yes? Then, I think we can do the following tests
on the webservice:

1. Creating a client proxy through the following guide(using wsdl.exe
rather than via VS.NET) and consume the webservice by the geneated proxy to
see whether the webservice still fail.
#Creating an XML Web Service Proxy
http://msdn.microsoft.com/library/e...atingWebService
Proxy.asp?frame=true

2. I'm not sure whether you have ever tried calling a webservice via
javascript(DHTML) in a webpage, if not, you may have a look at the
following reference:
#Accessing Web Services From DHTML
http://msdn.microsoft.com/library/e...001.asp?frame=t
rue

If you're able to perform a test via the DHTML way, we can further confirm
whether the problem is with the serverside or not.

In addtion, you can create a simple webservice( similiar with the one
you're consuming, maybe just take 2 string parameters) on your local
machine or a remote machine which you have full control on it. And then
call it on the local machine to see whether the same problem occurs. If
not, that also probably proof that the problem is likely due to the
webservice's serverside processing.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Hi Tim,

Any progress on this issue? If you have any problems on this or if there're
anything else I can help, please feel free to let me know. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

System.Net.WebException: The underlying connection was closed:

Hi Tim,

From your description, you've a ASP.NET webservcie and created a Winform
client app to consume it. However, when calling the webservice, you found
it worked well the first time but occured unexpected error the second time
after a few minutes, yes?

From the code you provided(calling the webservice), it seems all right and
I don't think the problem is likely in the client application. I'm not sure
about the webservice's detailed code logic in its webmethods but from the
method's signature:

WebSvcW.OMTestAddOrderXML(comboBox3.Text,int.Parse (textBox2.Text),comboBox4.
Text);

it takes simply type params, so I think we can test the webservice through
IE, open the certain url (http://...... .asmx) in the IE and call the
webservice thourgh it. It is better if you can use VS.NET to debug the
webservice on the serverside to see whether the problem also occured when
calling it from IE. If still remains, we can confirm that the problem is
not caused by the winform client , do you think so?

In addition, as for creating client application to consume webservice,
below is the related web link in MSDN:
#Creating Clients for XML Web Services
http://msdn.microsoft.com/library/e...atingclientsfor
webservices.asp?frame=true

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspxHi Tim,

Have you had a chance to view the suggestions in my last reply or have you
got any progresses on this issue? If there is anything else I can help,
please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Thanks for your reply. However, when I try to go directly to the ..asmx url via IA, when I click on the name of the web service in order to test, I receive this message - Tes
The test form is only available for requests from the local machine. Therefore, I am unclear on how to proceed. The server is in another state so I can't logon directly to it in order to test from my location. How shall I proceed

Thank you
Tim Reynold
Verizon
Hi Tim,

From your reply, you haven't the certain permission to logon the server
which host the webservice, yes? Then, I think we can do the following tests
on the webservice:

1. Creating a client proxy through the following guide(using wsdl.exe
rather than via VS.NET) and consume the webservice by the geneated proxy to
see whether the webservice still fail.
#Creating an XML Web Service Proxy
http://msdn.microsoft.com/library/e...atingWebService
Proxy.asp?frame=true

2. I'm not sure whether you have ever tried calling a webservice via
javascript(DHTML) in a webpage, if not, you may have a look at the
following reference:
#Accessing Web Services From DHTML
http://msdn.microsoft.com/library/e...001.asp?frame=t
rue

If you're able to perform a test via the DHTML way, we can further confirm
whether the problem is with the serverside or not.

In addtion, you can create a simple webservice( similiar with the one
you're consuming, maybe just take 2 string parameters) on your local
machine or a remote machine which you have full control on it. And then
call it on the local machine to see whether the same problem occurs. If
not, that also probably proof that the problem is likely due to the
webservice's serverside processing.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Hi Tim,

Any progress on this issue? If you have any problems on this or if there're
anything else I can help, please feel free to let me know. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

System.Net.WebException: The underlying connection was closed:

Hi Tim,

From your description, you've a ASP.NET webservcie and created a Winform
client app to consume it. However, when calling the webservice, you found
it worked well the first time but occured unexpected error the second time
after a few minutes, yes?

From the code you provided(calling the webservice), it seems all right and
I don't think the problem is likely in the client application. I'm not sure
about the webservice's detailed code logic in its webmethods but from the
method's signature:

WebSvcW.OMTestAddOrderXML(comboBox3.Text,int.Parse (textBox2.Text),comboBox4.
Text);

it takes simply type params, so I think we can test the webservice through
IE, open the certain url (http://...... .asmx) in the IE and call the
webservice thourgh it. It is better if you can use VS.NET to debug the
webservice on the serverside to see whether the problem also occured when
calling it from IE. If still remains, we can confirm that the problem is
not caused by the winform client , do you think so?

In addition, as for creating client application to consume webservice,
below is the related web link in MSDN:
#Creating Clients for XML Web Services
http://msdn.microsoft.com/library/e...atingclientsfor
webservices.asp?frame=true

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspxHi Tim,

Have you had a chance to view the suggestions in my last reply or have you
got any progresses on this issue? If there is anything else I can help,
please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Thanks for your reply. However, when I try to go directly to the ..asmx url via IA, when I click on the name of the web service in order to test, I receive this message - Tes
The test form is only available for requests from the local machine. Therefore, I am unclear on how to proceed. The server is in another state so I can't logon directly to it in order to test from my location. How shall I proceed

Thank you
Tim Reynold
Verizon
Hi Tim,

From your reply, you haven't the certain permission to logon the server
which host the webservice, yes? Then, I think we can do the following tests
on the webservice:

1. Creating a client proxy through the following guide(using wsdl.exe
rather than via VS.NET) and consume the webservice by the geneated proxy to
see whether the webservice still fail.
#Creating an XML Web Service Proxy
http://msdn.microsoft.com/library/e...atingWebService
Proxy.asp?frame=true

2. I'm not sure whether you have ever tried calling a webservice via
javascript(DHTML) in a webpage, if not, you may have a look at the
following reference:
#Accessing Web Services From DHTML
http://msdn.microsoft.com/library/e...001.asp?frame=t
rue

If you're able to perform a test via the DHTML way, we can further confirm
whether the problem is with the serverside or not.

In addtion, you can create a simple webservice( similiar with the one
you're consuming, maybe just take 2 string parameters) on your local
machine or a remote machine which you have full control on it. And then
call it on the local machine to see whether the same problem occurs. If
not, that also probably proof that the problem is likely due to the
webservice's serverside processing.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Hi Tim,

From your reply, you haven't the certain permission to logon the server
which host the webservice, yes? Then, I think we can do the following tests
on the webservice:

1. Creating a client proxy through the following guide(using wsdl.exe
rather than via VS.NET) and consume the webservice by the geneated proxy to
see whether the webservice still fail.
#Creating an XML Web Service Proxy
http://msdn.microsoft.com/library/e...atingWebService
Proxy.asp?frame=true

2. I'm not sure whether you have ever tried calling a webservice via
javascript(DHTML) in a webpage, if not, you may have a look at the
following reference:
#Accessing Web Services From DHTML
http://msdn.microsoft.com/library/e...001.asp?frame=t
rue

If you're able to perform a test via the DHTML way, we can further confirm
whether the problem is with the serverside or not.

In addtion, you can create a simple webservice( similiar with the one
you're consuming, maybe just take 2 string parameters) on your local
machine or a remote machine which you have full control on it. And then
call it on the local machine to see whether the same problem occurs. If
not, that also probably proof that the problem is likely due to the
webservice's serverside processing.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Hi Tim,

Any progress on this issue? If you have any problems on this or if there're
anything else I can help, please feel free to let me know. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Hi Tim,

Any progress on this issue? If you have any problems on this or if there're
anything else I can help, please feel free to let me know. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx