post方法时的服务器错误500

时间:2016-08-08 12:53:03

标签: c# post onvif

我面临着从c#制作po​​st方法的问题 我收到了500个内部服务器错误。

这是我的代码,它正在调用花时间,它包含xml文档。

我使用ARC插件进行了相同的调用,它工作正常。但是在.net中我遇到了问题

string Url = "http://10.107.2.153/onvif/device_service";
//System.Net.WebRequest req = System.Net.WebRequest.Create(Url);// + (string.IsNullOrEmpty(Data) ? "" : "?" + Data));
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
//req.Credentials = new NetworkCredential("admin","Cisco123");
req.Method = "POST";
req.ContentType = "text/xml;charset=UTF-8";//action=\"http://www.onvif.org/ver10/device/wsdl/GetSystemDateAndTime\"";
req.Headers.Add("SOAPAction","\"http://www.onvif.org/ver10/device/wsdl/GetSystemDateAndTime");
string postData = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"><s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><GetSystemDateAndTime xmlns=\"http://www.onvif.org/ver10/device/wsdl\"/></s:Body></s:Envelope>";
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(postData);//Encoding.UTF8.GetBytes(postData);
req.ContentLength = byteArray.Length;
Stream dataStream = req.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
try
{
    System.Net.HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException wex)
{
    WebResponse errResp = wex.Response;
    using (Stream respStream = errResp.GetResponseStream())
    {
        StreamReader reader = new StreamReader(respStream);
        string text = reader.ReadToEnd();
    }
}

1 个答案:

答案 0 :(得分:0)

通过添加protocolversion参数问题得到解决。

req.ProtocolVersion = HttpVersion.Version10

由于协议不匹配,可能会出问题。