开发者

trying to use vb.net to upload file to sinatra

开发者 https://www.devze.com 2023-04-03 15:29 出处:网络
I am trying to use VB.Net to upload a file to a Sinatra web service, and I\'m not sure how to configure either end.When I run the VB.Net app, sinatra invariably responds with code 404.Here\'s the VB.N

I am trying to use VB.Net to upload a file to a Sinatra web service, and I'm not sure how to configure either end. When I run the VB.Net app, sinatra invariably responds with code 404. Here's the VB.Net code, which I converted from another SO post:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     Dim responseData As String = ""
     Dim rdr As FileStream = New FileStream("X:\QueryTxtFiles\Query\CDA Curncy_9_1_2011.fqy", FileMode.Open)
     Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://finqueryserver:9898"), HttpWebRequest)
     req.Method = "POST" ' you might use "POST"
     req.ContentLength = rdr.Length
     req.AllowWriteStreamBuffering = True   

     Dim reqStream = DirectCast(req.GetRequestStream(), Stream)

     Dim inData(rdr.Length) As Byte

     ' Get data from upload file to inData 
     Dim bytesRead As Integer = rdr.Read(inData, 0, rdr.Length)

     ' put data into request stream
     reqStream.Write(inData, 0, rdr.Length)

     rdr.Close()
     Try
         req.GetResponse()
     Catch ex As Exception
         responseData = "An error occurred: " & ex.Message
     End Try

     ' after uploading close stream 
开发者_C百科     reqStream.Close()
 End Sub

And here is the Sinatra code:

 require 'rubygems'
 require 'sinatra'

 post '/:name/:filename' do
puts "got here"

   begin
     name = params[:name]
   rescue
     name = "no name"
   end  
   begin
     filename = params[:filename]
   rescue
     filename = "no filename"
   end  
 end

(I never see "got here".)The Sinatra code is based on tutorials using cURL, which I am not. I've also tried

 post "/:filename'

which gives 404 also, and simply

 post "/"

which does show "got here", but is obviously useless because I need to deal with the file.

Clearly I'm a newbie at both, this can't be that hard, but I'm not sure what to do.

Thank you.


Ok, well for posterity, here's what I did to get it to work. In the vb.net code, I used:

  qry_results = wc.UploadFile("http://192.168.9.81:9898/execfqy", "X:\QueryTxtFiles\Query\CDA Curncy_9_1_2011.fqy")

and in the sinatra file:

 post '/execfqy' do
 qry_file.write(params[:file][:tempfile].readlines)
0

精彩评论

暂无评论...
验证码 换一张
取 消