xml – vb.net中的REST调用:如何传递在cURL中工作的密钥“文件
我已经在这个问题上苦苦挣扎了差不多一个月了,我在网上找到了所有可以找到的东西,没有任何解决方案.这是我的问题:我正在为RESTful API服务实现一个客户端,该服务必须通过POST调用在vb.net中发送
XML文件.我能够以xml格式获取一些数据,但是当发送这个Xml文件时,我总是得到“400错误的请求错误”.
我已经发现它必须是必须传递给服务器的密钥的问题(显然只接受POST的文件上传,我不能将其作为字符串发送). 基本上这个调用与cURL一起使用,但我正在努力在vb.net中实现我自己的调用,传递正确的值. 工作cURL调用:(成功传输XML) c:>curl -u username:password -F "file=@filename.xml" -X POST http://hostname.com/URI?parameters 不工作的Vb.net代码:(这给了我400 Bad Request) Dim ss As String = "" 'server says... Dim S As String = txb_username.Text & ":" & txb_password.Text Dim EncodedString As String = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(S)) Dim req As HttpWebRequest = Nothing Dim res As HttpWebResponse = Nothing Try Dim xmlDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument xmlDoc.XmlResolver = Nothing xmlDoc.Load("c:pathfile4.xml") Dim sXML As String = "file" & xmlDoc.InnerXml '<- This is where I try to put the "KEY" Dim url As String = "http:/host.com+URI" req = CType(WebRequest.Create(url),Net.HttpWebRequest) 'or Directcast ... req.Method = "POST" req.Headers.Add("Authorization: Basic " & EncodedString) req.ContentType = "multipart/form-data" req.ContentLength = sXML.Length req.Accept = "*/*" System.Windows.Forms.Application.DoEvents() Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(req.GetRequestStream) StatusUpdate(sXML) sw.Write(sXML) sw.Close() ss = "server says: " res = CType(req.GetResponse,HttpWebResponse) StatusUpdate(req.ToString) Catch ex As Exception StatusUpdate(ss & ex.Message) Finally End Try 是因为我想把它作为字符串发送吗? (但我怎么能把它作为文件发送?) Dim requestStream As Stream = Nothing Dim fileStream As FileStream = Nothing Dim uploadResponse As Net.HttpWebResponse = Nothing Try Dim uploadRequest As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create(URI.Text & Uri_part2.text),Net.HttpWebRequest) uploadRequest.Method = Net.WebRequestMethods.Http.Post uploadRequest.ContentType = "text/xml; charset=utf-8" uploadRequest.Credentials = New NetworkCredential("user","pass") uploadRequest.KeepAlive = True uploadRequest.UserAgent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10" uploadRequest.Accept = ("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") uploadRequest.Headers.Add("Accept-Language: en-us,en;q=0.5") uploadRequest.Headers.Add("Accept-Encoding: gzip,deflate") uploadRequest.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7") uploadRequest.Headers.Add("Content-Disposition: form-data; name=""file"";") uploadRequest.ContentType = "application/xml; charset=utf-8" requestStream = uploadRequest.GetRequestStream() fileStream = File.Open("C:example.xml",FileMode.Open) Dim a As Integer Dim buffer(1024) As Byte Dim bytesRead As Integer While True a = a + 1 bytesRead = fileStream.Read(buffer,buffer.Length) StatusUpdate(buffer(a)) If bytesRead = 0 Then Exit While End If requestStream.Write(buffer,bytesRead) End While requestStream.Close() uploadResponse = uploadRequest.GetResponse() Dim responseReader As StreamReader = New StreamReader(uploadRequest.GetResponse.GetResponseStream()) Dim x As String = responseReader.ReadToEnd() responseReader.Close() StatusUpdate(x) Catch ex As UriFormatException StatusUpdate("UriFormatException: " & ex.Message) Catch ex As IOException StatusUpdate("IOException: " & ex.Message) Catch ex As Net.WebException StatusUpdate("Net.WebException: " & ex.Message) Finally If uploadResponse IsNot Nothing Then uploadResponse.Close() End If If fileStream IsNot Nothing Then fileStream.Close() End If If requestStream IsNot Nothing Then requestStream.Close() End If End Try 无论如何,我还尝试了其他2个客户端(POSTMAN和REST控制台,2个Google Chrome扩展程序),只有将值“file”添加到“key”字段中我才能使用它.我必须插入特定的4个字符“文件”才能使其正常工作.所以,问题是:如何在Vb.net调用中添加相同的值?如何在工作的Vb.net代码中翻译cURL调用的代码?非常感谢您的时间和帮助! 找到我想在这里添加的东西的图像, 附:我不能使用PUT,我必须使用POST(服务器限制) 我还添加了适用于我的目的的HTML代码,使用我的电脑上的服务器(再次参见“文件”键) <html> <body> <form enctype="multipart/form-data" action="http://URI" method="POST"> <table border=0> <tr> <td align="right">File </td> <td><input type="FILE" name="file"></td> </tr> <tr> <td> </td> <td><input type="submit"></td> </tr> </table> </form> </body> </html> 此外,我还在PERL中粘贴了一个脚本,该脚本也可以在我的计算机上与服务器一起使用. ?? #!perl use strict; use LWP; # Loads all important LWP classes my $client_id = 1234; my $filename = "new_file.xml"; ### Prepare to make a request my $browser = LWP::UserAgent->new; my $url = "http://uri.com?&xx=$client_id"; my @post_pairs = ( #'client_id_in' => $client_id,'file' => [$filename],); my @ns_headers = ( 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10','Accept' => 'text/html,*/*;q=0.8','Accept-Language' => 'en-us,en;q=0.5','Accept-Encoding' => 'gzip,deflate','Accept-Charset' => 'ISO-8859-1,*;q=0.7','Authorization' => 'Basic base64EncodedCredentialsHere','Content_Type' => 'form-data',); ### Make a request my $response = $browser->post($url,@post_pairs,@ns_headers); die "Can't get $url -- ",$response->status_line unless $response->is_success; ### Display the response print STDOUT $response->content; 解决方法
我认为你的问题是你真的不知道请求应该是什么样的,请使用fiddler来查看cURL发送的请求,并尝试使用vb.net实现相同的请求.在我看来,你的secons代码段(使用缓冲区,而不是xml序列化程序)应该可以工作……但只有在服务器端可以理解这种结构.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |