简体   繁体   中英

Simple http web post request not working properly from c# code, whereas same content from REST client is working fine

I have to request a URL ie " http://192.168.220.12:5000 " where a java service will receive it,

with following string as Body

<?xml version='1.0'?><!DOCTYPE svc_init SYSTEM 'ABCD.DTD'><svc_init ver='3.3.0'><hdr ver='3.3.0'><client><id>xxx</id><pwd>xxx</pwd></client></hdr><aaaa ver='3.3.0'><trans_id>1</trans_id><request_type type='2'/><l_hor_acc type='HIGH'/></aaaa></svc_init>

I am able to do it successfully with RESTClient from my firefox browser, see the image below: 在此处输入图片说明 But when I send it through following C# code it gives me following error:

 java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at Creader.run(Creader.java:42)

C# code is as follows:

xmlData= "<?xml version='1.0'?><!DOCTYPE svc_init SYSTEM 'ABCD.DTD'><svc_init ver='3.3.0'><hdr ver='3.3.0'><client><id>xxx</id><pwd>xxx</pwd></client></hdr><aaaa ver='3.3.0'><trans_id>1</trans_id><request_type type='2'/><l_hor_acc type='HIGH'/></aaaa></svc_init>";

address = "http://192.168.220.12:5000";

using (var client = new WebClient())
{
  client.UploadData(address , Encoding.ASCII.GetBytes(xmlData));
}

What am I doing wrong here?

I also tried following but nothing worked. 1) Convert xmlData to byte [] using another method . 2) used Encoding.UTF8.GetBytes instead of Encoding.ASCII.GetBytes. 3) used client.UploadString(new Uri(mlcAddress), xmlData) instead of client.UploadData...

            WebRequest request = WebRequest.Create("http://192.168.220.12:5000");
            request.Method = "POST";
            string xmlData= "<?xml version='1.0'?><!DOCTYPE svc_init SYSTEM 'ABCD.DTD'><svc_init ver='3.3.0'><hdr ver='3.3.0'><client><id>xxx</id><pwd>xxx</pwd></client></hdr><aaaa ver='3.3.0'><trans_id>1</trans_id><request_type type='2'/><l_hor_acc type='HIGH'/></aaaa></svc_init>";
            byte[] byteArray = Encoding.UTF8.GetBytes (xmlData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream ();
            dataStream.Write (byteArray, 0, byteArray.Length);
            dataStream.Close ();
            WebResponse response = request.GetResponse ();
            dataStream = response.GetResponseStream();                
            StreamReader reader = new StreamReader (dataStream);
            string responseFromServer = reader.ReadToEnd ();
            reader.Close ();
            dataStream.Close ();
            response.Close ();

Following code (TCP socket) worked

oSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                ProtocolType.Tcp);
System.Net.IPAddress oIPAddress = System.Net.IPAddress.Parse(ip);
System.Net.IPEndPoint oEndPoint = new System.Net.IPEndPoint(oIPAddress, port);
oSocket.Connect(oEndPoint);

Object oData = xmlData;
byte[] bData = System.Text.Encoding.ASCII.GetBytes(oData.ToString());
oSocket.Send(bData);

You need to compare the bytes on wire between your code and what the REST client is sending. It is possible that the REST client is sending headers & body in one packet, whereas .NET is sending request headers separately from the body, which is causing the server to throw because it is not expecting this.

Use wireshark to sniff the traffic, or if this is localhost, use Firebug on firefox, and for .net create a system.net trace log. Just google for "tracing with system.net" to get info on how to do it.

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