cancel
Showing results for 
Search instead for 
Did you mean: 

OData POST request returning 403 forbidden

Former Member
0 Kudos
10,198

Hi All,

I am trying to make a POST request from windows mobile 6.5 through SUP; I have included the headers X-CSRF-Token  and  X-SUP-APPCID. Please help me to correct the error in my code.

HttpWebRequest request =

            (HttpWebRequest)HttpWebRequest.Create(url);

            request.Credentials = cache;

           

            request.Method = "POST";

            request.ContentType = "application/atom+xml";

            request.Accept = "application/xml,application/atom+xml";

            request.AllowWriteStreamBuffering = true;

           

            

            request.Headers.Add("X-SUP-SC", "Security1");

            request.Headers.Add("X-CSRF-Token", XCSRFToken);

            request.Headers.Add("X-SUP-APPCID", ApplicationConnectionID);

            request.Headers.Add("X-SUP-URLREWRITE-ENABLED", "False");

            request.Headers.Add("Set-Cookie", strCookie);

            byte[] bytes = Encoding.UTF8.GetBytes(postData);

            request.ContentLength = bytes.Length;

            using (Stream putStream = request.GetRequestStream())

            {

                putStream.Write(bytes, 0, bytes.Length);

                putStream.Close();

            }

`                 WebResponse response = request.GetResponse();

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

I resolved the issue by formatting the cookie which is returning from the response.

Answers (7)

Answers (7)

former_member198924
Participant
0 Kudos

Hi Shine Gopal,

                          I'm also have same problem. System.Net.WebException: The remote server returned an error: (403) Forbidden. Could you please send your post request code so that I can refer.

And I didn't get about formatting cookie can you please elaborate because am new to this.

With Regards

Durai Venkatesh

Former Member
0 Kudos

Hi All,

i am develoing a native iOS application where I am trying to consume Odata services through Sybase Mobile Platform 2.3 and Relay server.

I have tested my Odata service url in rest client also and its working fine.

In my iOS code I am able to fetch the application end point but while fetching the service document I am getting this error SUPJsonException:expected string, found 173/Location:-[ODPRequest startSynchronous].

My code is

            NSString *urlStr = [ODPAppSettings getApplicationEndpointWithError:&error];

            NSURL *urlNew = [NSURL URLWithString:urlStr];

            [SDMRequestBuilder enableXCSRF:YES];

            id<SDMRequesting> request = [SDMRequestBuilder requestWithURL:urlNew];

            [request setRequestMethod:@"GET"];

            [request setUsername:@"xxxxxx"];

            [request setPassword:@"xxxxxx"];

            [request addRequestHeader:@"Content-Type" value:@"application/atom+xml;type=entry"];

            [request addRequestHeader:@"X-Requested-With" value:@"XMLHttpRequest"];

            [request startSynchronous];


My netweaver server version is 7.4. Could anyone please help me to resolve this issue.



With Regards

Neha Pandey

Former Member
0 Kudos

Hi Shine,

Good to hear that your issue got resolved.

Could you please share the code of entire webservice call including GET and POST method.

Regards

Sheetal

sujith_prathap
Advisor
Advisor
0 Kudos

Hi,

     Try Adding this additional header.

X-Requested-With : XMLHttpRequest

and check if it works.

Best Regards,

Sujith

Former Member
0 Kudos

Try removing these two lines from the request:

            request.ContentType = "application/atom+xml";

            request.Accept = "application/xml,application/atom+xml";

If I remember on the JavaScript side for WM 6.5, above two lines caused issues.  I would imagine the same code is being used when you try to do the same thing from C#.

Former Member
0 Kudos

Hi Andrew Lee,

I removed those lines and I got the http error "The remote server returned an error: (500) Internal Server Error."

Thanks.

midhun_vp
Active Contributor
0 Kudos

Can you use PUT instead of POST. What is the NW gateway version and patch level.

- Midhun VP

midhun_vp
Active Contributor
0 Kudos

I believe that you are requesting a bad url.

Please try the POST method in firefox restclient and make sure that the POST is working.

- Midhun VP

Former Member
0 Kudos

Hi Midhun,

I tried from the rest client "Postman" and its working fine.

midhun_vp
Active Contributor
0 Kudos

What is the error you are getting?

Are you passing the token and cookie correctly ?

- Midhun VP

Former Member
0 Kudos

Hi ,

Thanks for your response. Stack Trace is given below.

Before post request I send GET request and stored the XCSRF token and cookie as below

strXCSRFToken = response.Headers["X-CSRF-Token"];

strCookie =response.Headers["Set-Cookie"];

{System.Net.WebException: The remote server returned an error: (403) Forbidden.

   at System.Net.HttpWebRequest.finishGetResponse()

   at System.Net.HttpWebRequest.GetResponse()

   at WindowMobile3._5OData.Form1.PostBankDetail()

   at WindowMobile3._5OData.Form1.Form1_Load(Object sender, EventArgs e)

   at System.Windows.Forms.Form.OnLoad(EventArgs e)

   at System.Windows.Forms.Form._SetVisibleNotify(Boolean fVis)

   at System.Windows.Forms.Control.set_Visible(Boolean value)

   at System.Windows.Forms.Application.Run(Form fm)

   at WindowMobile3._5OData.Program.Main()

}