Click here to Skip to main content
15,899,565 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i get error while accessing rest service with C# code but it works on postman request

here is the code that i have written

it throws the error when it executes this code

HttpWebResponse response =(HttpWebResponse) request.GetResponse();


What I have tried:

C#
try
           {
               HttpWebRequest request =(HttpWebRequest) WebRequest.Create("https://smsmate.lk/api/sms/v2/send");
               request.Method = "POST";
               request.PreAuthenticate = true;

               request.Headers["Authorization"] = "ApiKey" + "XXXXXX-150a-4fae-9f43-ce6e49b92d4e";
               Mysms mymsg = new Mysms();
               mymsg.Message = "Test";
               mymsg.Addresses = "94719254547";
               // JsonConvert.DeserializeObject(mymsg);
               var postdata = "{" + "Message:" + mymsg.Message + "," + "Addresses:[" + mymsg.Addresses + "]}";
               byte[] byteArray = Encoding.UTF8.GetBytes(postdata);

               var data = Encoding.ASCII.GetBytes(postdata);

               //if (ContainsUnicodeCharacter(postdata))
               //{
               //    data = Encoding.UTF8.GetBytes(postdata);
               //}

               request.ContentType = "application/json";
               request.ContentLength = postdata.Length;
               Stream req = request.GetRequestStream();
               req.Write(byteArray, 0, byteArray.Length);
               req.Close();
               HttpWebResponse response =(HttpWebResponse) request.GetResponse();
               req = response.GetResponseStream();

           }
           catch (Exception ex)
           {
               throw ex;
           }
Posted
Updated 11-Jan-18 22:03pm
Comments
Richard Deeming 12-Jan-18 13:34pm    
catch (Exception ex)
{
    throw ex;
}


Don't do that; it destroys the exception's stack trace. If you really need to re-throw an exception, just use throw;:
catch (Exception ex)
{
    throw;
}


But in this case, since you're not doing anything with the exception, there's no point in catching it. Just remove the try..catch block, and let the exception propagate to the caller.
Richard Deeming 12-Jan-18 13:35pm    
If the server returns an error code, request.GetResponse() will throw a WebException.

Catch that exception and examine its Status and Response properties to see what error the server returned.

1 solution

Sounds like a very fragile API server... It should validate inbound requests and give error responses.

Use something like Fiddler (Free Web Debugging Proxy - Telerik)[^] to debug the actual data being send and compare with Postman. Chances are that you are not sending the exact same thing.
 
Share this answer
 
Comments
sumithasanka 12-Jan-18 4:08am    
can i upload api server document pdf here ?if so how ?
Graeme_Grant 12-Jan-18 4:53am    
No, you can't. You need to capture and debug the raw post from your code ... comparing with the raw post with Postman (which works) will give you your answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900