0

I get this Error :

Content-Length or Chunked Encoding cannot be set for an operation that does not write data

How can I resolve it?

Here is my code:

string xmlreq="<?xml version='1.0' encoding='iso-8859-1'?><methodCall><methodName>GetBalanceAndDate</methodName><params><param><value><struct><member><name>originNodeType</name><value><string>EXT</string></value></member><member><name>originHostName</name><value><string>FashionMasala</string></value></member><member><name>transactionType</name><value><string>FashionMasala</string></value></member><member><name>transactionCode</name><value><string>FashionMasala</string></value></member><member><name>externalData1</name><value><string>FashionMasala_VAS</string></value></member><member><name>externalData2</name><value><string>FashionMasala_VAS</string></value></member><member><name>originTransactionID</name><value><string>1</string></value></member><member><name>originTimeStamp</name><value><dateTime.iso8601>"+DateTime.UtcNow.ToString("o")+"</dateTime.iso8601></value></member><member><name>subscriberNumberNAI</name><value><int>1</int></value></member><member><name>subscriberNumber</name<value><string>923030025659</string></value></member></struct></value></param></params></methodCall>";

HttpWebRequest webrequest =(HttpWebRequest)WebRequest.Create(url);
webrequest.ContentType = "text/xml";
webrequest.Method = "GET";
webrequest.ContentLength =xmlreq.Length;
webrequest.Headers.Add("Authorization", "Basic " + oatuh);
HttpWebResponse webresponse =(HttpWebResponse)webrequest.GetResponse();
blackbishop
  • 15,559
  • 6
  • 43
  • 59
Majid Nawaz
  • 21
  • 1
  • 1

1 Answers1

1

You're not actually writing (or encoding) your data. Try something like this:

string xmlreq = ...
var data = Encoding.ASCII.GetBytes( xmlreq );

var webrequest = (HttpWebRequest)WebRequest.Create( url );
webrequest.ContentType = "text/xml";
webrequest.Method = "GET";
webrequest.ContentLength = data.Length;
webrequest.Headers.Add( "Authorization", "Basic " + oatuh );

using( var stream = webrequest.GetRequestStream() )
    stream.Write( data, 0, data.Length );

var webresponse = (HttpWebResponse)webrequest.GetResponse();

I went with ASCII because why not, you'll have to decide on the proper encoding obviously.

Edit: And also, as CodesInChaos points out, it doesn't make much sense to send data in a GET request. Are you sure you're not supposed to use POST?

Chris
  • 5,257
  • 12
  • 28
  • Surprisingly I did not find a prohibition to send a request body in a GET request in the http 1.1 spec. – CodesInChaos Apr 25 '15 at 14:25
  • Interesting. Guess it could make sense in a web service context where a good old query string isn't enough. Seems SOAP and REST can use both POST and GET. – Chris Apr 25 '15 at 14:28
  • 1
    Related question: [HTTP GET with request body](http://stackoverflow.com/questions/978061/http-get-with-request-body), according to which you are allowed send a request body in a GET request, but it's not useful to do so. – CodesInChaos Apr 25 '15 at 14:49
  • i write this code but its given error: Unable to connect to the remote server – Majid Nawaz Apr 27 '15 at 08:55
  • And you know that the url is valid? – Chris Apr 27 '15 at 11:16
  • yes that is my api make in php i want to covert that code in c# – Majid Nawaz Apr 28 '15 at 07:16
  • now its give this error The underlying connection was closed: An unexpected error occurred on a receive – Majid Nawaz Apr 28 '15 at 07:30