Problemas para enviar XML al servicio de descanso

I'm working with a REST web service that uses basic authentication and returns me an XML string. Here is the method that I use to get the data and return it as an XDocument:

        var req = (HttpWebRequest)WebRequest.Create(uri);
        String readToEnd;
        const string postData = "";
        var encoding = new ASCIIEncoding();
        byte[] byte1 = encoding.GetBytes(postData);

        req.Method = "POST";
        req.Timeout = 10000;
        req.ContentType = "text/XML";
        req.ContentLength = byte1.Length;

        string authInfo = userName + ":" + password;

        authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
        req.Headers["Authorization"] = "Basic " + authInfo;

        var newStream = req.GetRequestStream();
        newStream.Write(byte1, 0, byte1.Length);

            var resp = req.GetResponse();
            var answer = resp.GetResponseStream();
            var _answer = new StreamReader(answer);
            readToEnd = _answer.ReadToEnd();
        catch (Exception ex)
            readToEnd = null;

        return readToEnd != null ? XDocument.Parse(readToEnd) : null;

Now later on, I've manipulated that xml and I'm ready to post it back to another uri. I would think it would be the same code except maybe putting my new XML string inside the variable 'postData'?

Is this the correct way to post an xml string to a webservice? I've looked but cant seem to shed any light on this when basic authorization is being used.

preguntado el 01 de febrero de 12 a las 04:02

Please see the FAQ regarding signatures in questions. -

You failed to state the actual issue your encountering. Please elaborate. -

Ok, hopefully I addressed both your issues. -

Is there a reason you are worrying about the REST plumbing yourself rather than using a library that will do it for you like RestSharp? -

Well, I didnt know RestSharp existed. I'll check it out. But I would like to know how to do the plumbing in case my company wont let me use a 3rd party dll. -

1 Respuestas

I do the following. The key is that you have to write your data to the request stream before posting. Hope it helps.

 var request = CreateBaseRequest(body);
        HttpWebResponse WebResp = (HttpWebResponse)request.GetResponse();
        Stream Answer = WebResp.GetResponseStream();
        StreamReader response = new StreamReader(Answer);
        var r = response.ReadToEnd();

 static HttpWebRequest CreateBaseRequest(string postData)

        var req = (HttpWebRequest)HttpWebRequest.Create(@"");

        req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
        req.Method = "POST";
        req.KeepAlive = true;

        byte[] buffer = Encoding.ASCII.GetBytes(postData);

        req.ContentLength = buffer.Length;
        Stream PostData = req.GetRequestStream();
        PostData.Write(buffer, 0, buffer.Length);

        return req;


Respondido 01 Feb 12, 11:02

Yeah unfortunatly, I'm having no luck with this. I'll run this code with my xml being in the body variable and var r just returns me "". I cant add service reference since the service accepts username/password in header of request. So I'm not sure whats going on server side since I didnt write that part. But otherwise, your code looks like it might do the job. - Jason

I assume you changed the ContentType to "text/XML" - my example is for json. But that might not be the problem. What do you expect r to contain? It is the response to your post, so perhaps the post is working but not returning anything? Another thing I can suggest is to monitor this in fiddler. Your first step would be to inspect your request to make sure it is formed, and then examine the response to see if there is anything, and to check the http status code. - user381624

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.