简体   繁体   中英

Consuming REST service with C# code

I am using the following code to get the json result from the service. It works fine for get methods. But when the method type is POST the request address changes to the previous address.

ie;

on the first call to this method the request.address= XXXXX.com:1234/xxx/oldv1.json (method type is get )

and it returns a json string from which I extract another address: XXXXX.com:1234/xxx/newv1.json and now I call the makerequest method with this endpoint and method type POST, contenttype="application/x-www-form-urlencoded".

When I put breakpint at using (var response = (HttpWebResponse)request.GetResponse()) and checked the request.address value, it was XXXXX.com:1234/xxx/newv1.json

But after that line is executed, the address changes to XXXXX.com:1234/xxx/oldv1.json and the function returns the same response I got with the first Endpoint( XXXXX.com:1234/xxx/oldv1.json ). Can anybody tell what I am doing wrong here?

Is there any better method to consume the service with POST method?

public string MakeRequest(string EndPoint,string Method, string contentType)
            {
                var request = (HttpWebRequest)WebRequest.Create(EndPoint);

                request.Method = Method;
                request.ContentLength = 0;
                request.ContentType =contentType;

                if ( Method == HttpVerb.POST)
                {
                    var encoding = new UTF8Encoding();
                    var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes("username=123&password=123");
                    request.ContentLength = bytes.Length;

                    using (var writeStream = request.GetRequestStream())
                    {
                        writeStream.Write(bytes, 0, bytes.Length);
                    }
                }
                using (var response = (HttpWebResponse)request.GetResponse())// request.address changes at this line on "POST" method types
                {
                    var responseValue = string.Empty;

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                        throw new ApplicationException(message);
                    }

                    // grab the response
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                            using (var reader = new StreamReader(responseStream))
                            {
                                responseValue = reader.ReadToEnd();
                            }
                    }

                    return responseValue;
                }

EDIT: Yesterday I asked THIS Question about consuming the service at client side and many suggested it needs to be done at server side as the other domain might not allow accessing the json result at client side.

The issue was about cookies. As I forgot to set the cookies, the request was getting redirected. I had to set cookie container by using

 request.CookieContainer = new CookieContainer();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM