简体   繁体   English

简单的http Web发布请求无法通过c#代码正常运行,而来自REST客户端的相同内容可以正常运行

[英]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, 我必须请求一个URL,即“ http://192.168.220.12:5000”,Java服务将在其中接收它,

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: 我可以通过Firefox浏览器使用RESTClient成功完成此操作,请参见下图: 在此处输入图片说明 But when I send it through following C# code it gives me following error: 但是,当我通过以下C#代码发送它时,出现以下错误:

 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: C#代码如下:

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 . 1)使用另一种方法将xmlData转换为byte []。 2) used Encoding.UTF8.GetBytes instead of Encoding.ASCII.GetBytes. 2)使用Encoding.UTF8.GetBytes而不是Encoding.ASCII.GetBytes。 3) used client.UploadString(new Uri(mlcAddress), xmlData) instead of client.UploadData... 3)使用client.UploadString(new Uri(mlcAddress),xmlData)代替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 以下代码(TCP套接字)有效

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. 您需要比较代码和REST客户端发送的内容之间的在线字节。 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. REST客户端有可能在一个数据包中发送标头和正文,而.NET则与正文分开发送请求标头,这导致服务器抛出异常,因为它不期望这样做。

Use wireshark to sniff the traffic, or if this is localhost, use Firebug on firefox, and for .net create a system.net trace log. 使用wireshark嗅探流量,或者如果这是本地主机,则在firefox上使用Firebug,并为.net创建一个system.net跟踪日志。 Just google for "tracing with system.net" to get info on how to do it. 只是谷歌为“与system.net跟踪”以获取有关如何执行此操作的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Post Rest Web API在Postman上工作,但未从C#项目中的代码返回任何响应 - Post rest web api working from Postman but not returning any response from code in C# project 使用rest客户端应用程序发送Web请求,但不使用c# - Send Web request working with rest client app, but not working with c# 我有在天蓝色上烫过的网络API。 从邮递员那里得到请求,然后简单地得到请求。 但它不能从C#http get调用 - I have web api which is hoted on azure. get request working from postman and simple get request. But it not working from c# http get call c# Httpclient 请求在 Windows 10 上工作正常返回 403 forbidden on windows 7(相同的代码) - c# Httpclient request working fine on windows 10 returns 403 forbidden on windows 7 (same code) 休息客户端发布请求适用于邮递员,但不适用于 C# 代码 - Rest client post request works in postman but not in c# code 在 postman 上发布 Http 调用,但代码在 C# 中不起作用 - Post Http call working on postman but code not working in C# 代码无法从C#中的数据库正常运行datareader - Code not working properly datareader from database in C# 简单的 C# 应用程序无法正常工作,在 Visual Studio 中运行时工作正常 - Simple C# application not working correctly, works fine when run from within Visual Studio ASP.Net Web API HTTP Verb URL无法从REST客户端运行 - ASP.Net Web API HTTP Verb URL not working from REST Client C# - 无法让这个看似简单的 Web 请求正常工作 - C# - Can't get this seemingly simple web request working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM