Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tranlating Java code to VBA

Status
Not open for further replies.

Bilberry

Programmer
Dec 17, 2007
111
NL
Dear friends,

I have got the Java code from the vendor to post a XML message with a JPG file to the webservice. Here you can find the Java code:

Code:
import java.io.File;

import javax.ws.rs.core.MediaType;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

public class MultipartRecord
{
    public static void main(String[] args)
    {
        try
        {
            File f = new File("c:\\image.jpg");
            
            HttpClient client = new HttpClient();

                PostMethod postMethod = new 
                PostMethod("[URL unfurl="true"]http://localhost/networking/rest/record/HOMEPAGE/");[/URL]
            
                postMethod.setRequestHeader("Cookie", "JSESSIONID=A741270662A86F796DA16646F0708C43");

            FilePart filePart = new FilePart("image_field", f);
            filePart.setContentType(MediaType.APPLICATION_OCTET_STREAM);

            StringPart sp = new StringPart("__xml_data__",
              "<platform><record><field1>Some text field</field1>"
            + "<image_field>image.jpg</image_field></record></platform>");

            sp.setContentType(MediaType.APPLICATION_XML);
            
            final Part[] parts = { sp, filePart };

            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

            int executeMethod = client.executeMethod(postMethod);
        }
        catch(Exception e)
        {
            System.out.println("Exception");
        }
    }
}

I have tried to tranlate this but without success, anybody know how to post the stuff like the code above?

This is the part of my code:
Code:
Public Sub main()
    Dim query As String
    Dim strUrl As String
    Dim strUserName As String
    Dim strPassword As String
    Dim strUpload As String
    Dim strFile
    Dim myDom As MSXML2.DOMDocument
    Dim myDomResult As MSXML2.DOMDocument
    Dim objNodeList As IXMLDOMNodeList
    Dim str As String
    
    Set myDom = CreateObject("MSXML2.DOMDocument")
    Set myDomResult = CreateObject("MSXML2.DOMDocument")
    'Load entire Document before moving on
     myDom.async = False

     strWebAddress = "localhost/networking/rest/record/HOMEPAGE"
     strUrl = "[URL unfurl="true"]https://"[/URL] & strWebAddress
    
    Set objHttp = CreateObject("MSXML2.XMLHTTP.6.0")
    

    Dim myFSO, f
    Set myFSO = CreateObject("Scripting.FileSystemObject")
    Set f = myFSO.getFile("C:\image.jpg")
    
strUpload = "<platform><record><field1>Example</field1>" & _
            "<image_field>" & f & "</image_field></record></platform>"

myDom.loadXML (strUpload)

  
    
    
   objHttp.Open "POST", strUrl, False
    
  objHttp.setRequestHeader "Content-Type", "application/xml"

'This is the difficult part??
objHttp.send (myDom.XML)
'objHttp.send "__xml_data__" & (myDom.XML) & "image_field" & f

result2 = objHttp.responseText

Range("a3") = (result2)


End Sub

Im struggling with the objHttp.send (myDom.XML) part and dont know how to add the XML and the file to the send commando like in the java code. Could somebody help me with this code? Three golden stars for the final solution -:)
 
You posted this in the vb.net forum and referenced VB6.
You posted in the VB6 forum and are referencing VBA.

I think you need to determine what language you are dealing with in order to received a proper response.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top