Reputation: 1674
I'm trying to send custom exceptions from ASP.NET Web API , but when I consume these WebService from Android I'm always getting a different message:
This how I read the webservice in Android:
public Object doRequest(String url) {
String charset = Charset.defaultCharset().displayName();
try {
if (mFormBody != null) {
// Form data to post
mConnection
.setRequestProperty("Content-Type",
"application/json; charset="
+ charset);
mConnection.setFixedLengthStreamingMode(mFormBody.length());
}
mConnection.connect();
if (mFormBody != null) {
OutputStream out = mConnection.getOutputStream();
writeFormData(charset, out);
}
// Get response data
int status = mConnection.getResponseCode();
if (status >= 300) {
String message = mConnection.getResponseMessage();
return new HttpResponseException(status, message);
}
InputStream in = mConnection.getInputStream();
String enconding = mConnection.getContentEncoding();
if (enconding == null) {
enconding = "UTF-8";
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
in, enconding));
StringBuilder sb = new StringBuilder();
String line=null;
while ((line=reader.readLine()) != null) {
sb.append(line);
}
return sb.toString().trim();
} catch (Exception e) {
return e;
}
finally{
if(mConnection!=null){
mConnection.disconnect();
}
}
}
As you can see, I check the value returned by getResponseCode()
and if it's equal or greater than 300 I throw an Exception. Everything works except for the fact that getResponseMessage()
doesn't return the string I used when I created the exception in WebApi. Instead I get this error:
From WebApi,all I'm doing in the catch
block is to throw the exception:
try
{
}
catch (Exception ex)
{
throw ex;
}
Using fiddler realized I was getting this message:
{"Message":"Error."}
Well, looking for a solution on Internet, I found that I could do something like this:
try
{
}
catch (Exception ex)
{
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
//throw (ex);
}
But unfortunately this didn't work either. Although fiddler now shows the message I used to create the Exception.
When I read the value of
getResponseMessage()
is returns this string : "Not Found".
Do you know what I need to do so that the message that is being sent from WebApi through the Exception
make its way to Android, specifically to the getResponseMessage()
property?
Thanks in advance.
Upvotes: 2
Views: 559
Reputation: 4413
Well, I think you need to create a HttpResponseMessage
object first, and based on that object create the HttpResponseException
that you're going to throw.
Setting a HttpResponseMessage object is pretty easy. Most of the time you only need to set two properties: Content
and ReasonPhrase
.
try
{
}
catch (Exception ex)
{
HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("Excepción")),
ReasonPhrase = ex.Message
};
throw new HttpResponseException(msg);
}
As you can see is ReasonPhrase
where we passed the exception message.
Hope it helps.
Upvotes: 1