使用POST方法发送XML Webrequest(http 1.1) [英] Send XML Webrequest with POST method (http 1.1 )

查看:111
本文介绍了使用POST方法发送XML Webrequest(http 1.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在POST此xml时遇到问题.

 HttpWebResponse响应=(HttpWebResponse)request.GetResponse())// 返回403 FORBIDDEN.  


我真的不知道我在要求中想念什么.如果我与Wfethc一起发送,我的XML已经过验证,也可以使用,我也可以通过Fiddler调试请求.
如果我尝试连接https,那么提琴手将显示状态200 OK.但是我的程序仍然返回403 Forbidden.
根据HTTP 1.0或HTTP 1.1规范,接口需要标头和内容两个部分,标头部分指定HTTP方法(POST),内容类型(text/xml),内容长度等. UserID和密码通过Authorization标头作为Base64coder字符串发送. HTTP消息的内容是一个XML文档,其中包含调用接口所需的参数.

请注意,XML文档的默认编码方案不支持超出标准ASCII设置范围0x00-0x7F的字符.因此,如果打算发布包含带有重音符号,变音符号或其他变音符号的拉丁字符的XML文档,则应使用encoding ="utf-8".

 公共 无效 SendXML(XmlDocument doc)
       {
           Uri地址=  Uri(" );
           HttpWebRequest request =(HttpWebRequest)WebRequest.Create(address);
           MemoryStream ms =  MemoryStream();
           doc.Save(ms);
           字符串 id = " ;
           字符串密码= " ;
           字符串凭据= id + "  +密码;
           字符串 basicAuth = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));
           request.ContentType = " ;
           request.Headers.Add("  + basicAuth);
           request.Method = " ;
           字节 [] byteArray = ms.ToArray();
           request.ContentLength = byteArray.Length;
           request.Accept = " ;
           request.KeepAlive =  false ;
           request.ServicePoint.Expect100Continue =  false ;
           request.PreAuthenticate =  false ;
           //  byte [] bytes = ms.ToArray(); 
           //  MessageBox.Show(Encoding.UTF8.GetString(bytes)); 
           //  WebProxy代理=新的WebProxy("http://edi.samsungservice.co.uk:80"); 
           //  request.Proxy = proxy; 
           使用( var  writeStream = request.GetRequestStream())
           {
               writeStream.Write(byteArray.ToArray(), 0 ,byteArray.Length);
               //  writeStream.Close(); 
           }
           尝试
           {
               使用(HttpWebResponse response =(HttpWebResponse)request.GetResponse())// 使用(流responseStream = response.GetResponseStream())
                   {
                       使用(StreamReader reader =  StreamReader(responseStream))
                       {
                           字符串 responseFromServer = reader.ReadToEnd();
                           // 显示内容.
                           MessageBox.Show(responseFromServer);
                       }
                   }
               }
           }
           捕获(System.Net.WebException e)
           {
                var  result = e.Status.ToString();
               MessageBox.Show(结果," );
           }
       }


如果有人可以帮助我,请.
再见
AV

解决方案

我认为问题不在代码中.它的安全性问题.您的代码没有足够的权限来连接所需的服务器.我认为您需要提供适当的许可.
请提供有关错误消息的更多详细信息.


这是我的错误.

System.dll中发生了类型为"System.Net.WebException"的未处理的异常
附加信息:远程服务器返回错误:(403)禁止.

我在哪里可以更改权限?


在哪里必须在Visual Studio中为代码或服务器设置权限(我不能更改它)?


I have problem with POST this xml.

HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //return 403 FORBIDDEN. 


I realy don''t know what i miss in request. My XML is validated and also work if i send it with Wfethc, I also debug request with Fiddler.
If i try connect https then fiddler show status 200 OK. But my program still return 403 Forbidden.
Interface require both header and content sections with the header section specifying the HTTP method (POST), content type (text/xml), content-length, and so on, as per HTTP 1.0 or HTTP 1.1 specification. The UserID and Password is sent via the Authorisation header as Base64coder string. The content of the HTTP message is a XML document containing the parameters required to invoke the interface.

Please note that the default encoding scheme for XML documents does not support characters outside the standard ASCII set range of 0x00 - 0x7F. Therefore, if you intend to post XML documents containing Latin characters with accents, umlauts, or other diacritics, you should use encoding="utf-8".

public void SendXML(XmlDocument doc)
       {
           Uri address = new Uri("http://.../putServiceResponse");
           HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
           MemoryStream ms = new MemoryStream();
           doc.Save(ms);
           string id = "User";
           string password = "pass";
           string credentials = id + ":" + password;
           string basicAuth = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));
           request.ContentType = "text/xml";
           request.Headers.Add("Authorization: Basic " + basicAuth);
           request.Method = "POST";
           byte[] byteArray = ms.ToArray();
           request.ContentLength = byteArray.Length;
           request.Accept = "text/xml";
           request.KeepAlive = false;
           request.ServicePoint.Expect100Continue = false;
           request.PreAuthenticate = false;
           //byte[] bytes = ms.ToArray();
           //MessageBox.Show(Encoding.UTF8.GetString(bytes));
           //WebProxy proxy = new WebProxy("http://edi.samsungservice.co.uk:80");
           //request.Proxy = proxy;
           using (var writeStream = request.GetRequestStream())
           {
               writeStream.Write(byteArray.ToArray(), 0, byteArray.Length);
               //writeStream.Close();
           }
           try
           {
               using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //return 403 FORBIDDEN
               {
                  MessageBox.Show(((HttpWebResponse)response).StatusDescription);
                   using (Stream responseStream = response.GetResponseStream())
                   {
                       using (StreamReader reader = new StreamReader(responseStream))
                       {
                           string responseFromServer = reader.ReadToEnd();
                           // Display the content.
                           MessageBox.Show(responseFromServer);
                       }
                   }
               }
           }
           catch (System.Net.WebException e)
           {
               var result = e.Status.ToString();
               MessageBox.Show(result, "Error");
           }
       }


Please if anyone could help me.
Bye
AV

解决方案

I think the problem is not in the code.Its security problem.Your code dont have enough permission to connect the desired server.I think you need to give proper permission.
Please give more details about the error message.


This is my error.

An unhandled exception of type ''System.Net.WebException'' occurred in System.dll
Additional information: The remote server returned an error: (403) Forbidden.

where can i change permissions?


where must set permission in visual studio for the code or on server(i can''t change it)?


这篇关于使用POST方法发送XML Webrequest(http 1.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆