Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java to VB.Net help

Options
  • 08-07-2014 9:15pm
    #1
    Registered Users Posts: 1


    Hi all,

    I have an API for a web application which I need some help with. I need to run it in .NET rather than Java.

    The snippet provided below is an example of how you can append "Attachments to Requests" written in java.

    I have the following Java snippet.....................

    {

    /**

    * 1. Create a MultipartPostMethod

    * 2. Construct the web URL to connect to the SDP Server

    * 3. Add the filename to be attached as a parameter to the MultipartPostMethod with parameter name "filename"

    * 4. Execute the MultipartPostMethod

    * 5. Receive and process the response as required

    * /

    HttpClient client = new HttpClient( );

    String weblinkURL = "http://<SDPServer>:<PortNumber>/sdpapi/request/<requestId>/attachment?OPERATION_NAME=ADD_ATTACHMENT&TECHNICIAN_KEY=<general technician API key>";

    MultipartPostMethod method = new MultipartPostMethod( weblinkURL );

    String fileName = "C:" + File.pathSeparator + "ManageEngine" + File.pathSeparator + "ServiceDesk" + File.pathSeparator + "a.csv";

    File file = new File(fileName);

    method.addParameter("filename", file );

    try {

    client.executeMethod( method );

    String response = method.getResponseBodyAsString();

    System.out.println( response );

    } catch (HttpException he) {

    System.out.println( he );

    } catch (Exception e) {

    System.out.println( e );

    } finally {

    method.releaseConnection( );


    }

    }


    I'm really interested in finding out how I can rewrite the lines in bold in .NET.

    Thanks for any help in advance.


Comments

  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Type "MultiPartPostMethod java" into google. Also please use code tags when putting code here, makes it easier to read.


  • Registered Users Posts: 1,712 ✭✭✭neil_hosey


    youd do something like this:

    WebRequest request = WebRequest.Create(weblinkURL );

    byte[] byteArray = Encoding.UTF8.GetBytes(MYDATA);

    stream= GetRequestStream(byteArray.Length, request, credentials);

    stream.Write(byteArray, 0, byteArray.Length);

    var response = (HttpWebResponse)request.GetResponse();



    Hopefully I understood the question corrrectly :/





    PS the GetRequestStream() method:

    private Stream GetRequestStream(int arraylen, WebRequest request, credentials creds)
    {
    String encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(creds));
    request.Headers.Add("Authorization", "Basic " + encoded);
    request.Method = "POST";
    request.ContentType = "text/xml";
    request.ContentLength = arraylen;

    return request.GetRequestStream();

    }


Advertisement