REST calls in XML – VB.NET: How to pass key “file” working in CURL

I have been struggling with this problem for almost a month, and I found everything I can find on the Internet without any solution. This is my problem: I am Implement a client for RESTful API service, the service must send XML file in vb.net via POST call. I can get some data in xml format, but when sending this Xml file, I always get “400 Bad request Error”.

I have found that it must be a key that must be passed to the server (obviously only accept POST file upload, I can not send it as a string).

Basically this call is used with cURL, but I am trying to implement my own call in vb.net, passing the correct value.

Working cURL call: (Successfully transferred XML)< /p>

c:>curl -u username:password -F "[email protected]" -X POST http://hostname.com/URI?parameters

Vb.net code not working: (This gives me 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:\path\file4.xml")

D im sXML As String = "file" & xmlDoc.InnerXml'<- This is where I try to put the "KEY"

Dim url As String = "http:/host.com+URI"< br />
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

It’s because of me Want to send it as a string? (But how can I send it as a file?)
For this, I did another process of sending data bytes, but this one also gave me “400” because (I assume) I didn’t put the “file “Key.

Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As Net.HttpWebResponse = Nothing
< br /> 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")
up loadRequest.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, 0, buffer.Length)
StatusUpdate(buffer(a))

If bytesRead = 0 Then
Exit While
End If
requestStream.Write( buffer, 0, bytesRead)

End While

requestStream.Close()

uploadResponse = uploadRe quest.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

In any case, I also tried 2 other clients (POSTMAN and REST console, 2 Google Chr ome extension), I can use it only by adding the value “file” to the “key” field. I have to insert the specific 4 characters “file” to make it work. So, the question is: how to do it in Vb.net Add the same value in the call? How to translate the code called by cURL in the working Vb.net code? Thank you very much for your time and help!

Find the image of what I want to add here,

Attachment: I can’t use PUT, I must use POST (server limit)

I also added To get the HTML code suitable for my purpose, use the server on my computer (see “File” button again)


< br />











File 
 



In addition, I also pasted a script in PERL, the script It can also be used with the server on my computer.

#!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',< br />'Accept' =>'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' =>'en- us,en;q=0.5',
'Accept-Encoding' =>'gzip,deflate',
'Accept-Charset' =>'ISO-8859-1,utf-8;q= 0.7,*;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;

< div class="content-split">

I think your problem is that you really don’t know what the request should look like. Please use fiddler to view the request sent by cURL, and Try to use vb.net to achieve the same request. In my opinion, your secons code snippet (using a buffer, not an xml serializer) should work…but only on the server side can understand this structure.

I have been struggling with this issue for almost a month, I found everything I can find online without any solution. This is my problem: I am implementing a client for a RESTful API service, which must send an XML file in vb.net via a POST call. I was able to Get some data in xml format, but when sending this Xml file, I always get “400 Bad Request Error”.

I have found that it must be a key that must be passed to the server (Obviously only accept POST file uploads, I can’t send it as a string).

Basically this call is used with cURL, but I am trying to implement my own in vb.net Call, pass the correct value.

Working curl call: (Transfer XML successfully)

c:>curl -u username:password -F "file [email protected]" -X POST http://hostname.com/URI?parameters

Vb.net code not working: (this gave me 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:\path\file4.xml")< br />
Dim sXML As String = "file" & xmlDoc.InnerXml'<- This is wh ere 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

Is it because I want to send it as a string? (But how can I send it as a file?)
For this, I did another process of sending data bytes, but this one also gave me “400” because (I assume) I didn’t put the “file “Key.

Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As Net.HttpWebResponse = Nothing
< br /> 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")
uplo adRequest.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, 0, buffer.Length)
StatusUpdate(buffer(a))

If bytesRead = 0 Then
Exit While
End If
requestStream.Write( buffer, 0, bytesRead)

End While

requestStream.Close()

uploadResponse = uploadRequ est.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

In any case, I also tried 2 other clients (POSTMAN and REST console, 2 Google Chrome Extension), I can use it only by adding the value “file” to the “key” field. I have to insert the specific 4 characters “file” to make it work. So, the question is: how to call it in Vb.net To add the same value? How to translate the code called by cURL in the working Vb.net code? Thank you very much for your time and help!

Find the image of what I want to add here,

Attachment: I can’t use PUT, I must use POST (server limit)

I also added To get the HTML code suitable for my purpose, use the server on my computer (see “File” button again)


< br />











File 
 



In addition, I also pasted a script in PERL, the script It can also be used with the server on my computer.

#!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_he aders = (
'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,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' =>'en-us, en;q=0.5',
'Accept-Encoding' =>'gzip,deflate',
'Accept-Charset' =>'ISO-8859-1,utf-8;q=0.7, *;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;

I think your problem is that you really don’t know what the request should look like, please use fiddler to view the request sent by cURL and try to achieve the same request using vb.net. In my opinion , Your secons code snippet (using a buffer, not an xml serializer) should work…but only on the server side can understand this structure.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 6005 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.