Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java to VB.Net help

  • 08-07-2014 09:15PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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