Traduzione da java

venerdì 28 aprile 2006 - 14.56

d@dora Profilo | Senior Member

ragazzi ho bisogno di una mano, devo tradurre da java questo pezzo di codice, ma la mia ignoranza me lo impedisce :-((( chi mi puo' dare una mano ???

String xml = new String("<CiscoIPPhoneExecute>"+
"<ExecuteItem Priority=\"0\" URL=\"RTPRx:Stop\"/>"+"<ExecuteItem Priority=\"1\" URL=\"RTPRx:10.1.15.10:23480\"/>"+
"</CiscoIPPhoneExecute>");

String userId = "Max",
password = "12345",
basicAuth = "Basic ",
params = "XML=" + URLEncoder.encode(xml, "ISO-8859-1" );
byte[] bytes = params.getBytes();

// Create a URL pointing to the servlet or CGI script and open an HttpURLConnection on that URL
URL url = new URL( "http://10.1.15.56/CGI/Execute" );
HttpURLConnection con = ( HttpURLConnection ) url.openConnection();
// Indicate that you will be doing input and output, that the method is POST, and that the content
// length is the length of the byte array
con.setDoOutput( true );
con.setDoInput( true );
con.setRequestMethod( "POST" );
con.setRequestProperty( "Content-length", String.valueOf( bytes.length ) );

// Create the Basic Auth Header

Base64 encoder = new Base64();
basicAuth = (String)encoder.encode( ((String)(userId + ":" + password)).getBytes() );

System.out.println("Codificado: " + basicAuth);

con.setRequestProperty( "Authorization", "Basic " + basicAuth.trim() );

// Write the parameters to the URL output stream
OutputStream output = con.getOutputStream();
output.write( bytes );
output.flush();



ciao e grazie di cuore

Brainkiller Profilo | Guru

>ragazzi ho bisogno di una mano, devo tradurre da java questo
>pezzo di codice, ma la mia ignoranza me lo impedisce :-((( chi
>mi puo' dare una mano ???

Penso che pochi qui lavorano con Java.
In ogni caso non mi sembra molto complesso.
Ciao

David De Giacomi
Microsoft MVP
http://blogs.dotnethell.it/david/

d@dora Profilo | Senior Member

se ho chiesto aiuto e' perche' non sono in grado di tradurlo.......

Pongo Profilo | Senior Member

Eccolo! ^_^
mi da un errore di connesione alla fine perchè evidentemente non trova la roba a cui si deve connettere!
PS: Non ho guardato il codice e quindi non so che cosa faccia, vedi se ti va bene così! ^_^

/*
* Created on 29-apr-2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Prova;

import java.net.*;
import java.io.*;

import sun.misc.*;



/**
* @author Pongo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Test {

public static void main(String[] args) throws IOException {


String xml = new String("<CiscoIPPhoneExecute>"+
"<ExecuteItem Priority=\"0\" URL=\"RTPRx:Stop\"/>"+"<ExecuteItem Priority=\"1\" URL=\"RTPRx:10.1.15.10:23480\"/>"+
"</CiscoIPPhoneExecute>");

String userId,
password,
basicAuth,
params;
try {
userId = "Max";
password = "12345";
basicAuth = "Basic ";
params = "XML=" + URLEncoder.encode(xml, "ISO-8859-1" );
byte[] bytes = params.getBytes();
// Create a URL pointing to the servlet or CGI script and open an HttpURLConnection on that URL
URL url;
try {
url = new URL( "http://10.1.15.56/CGI/Execute" );
HttpURLConnection con = ( HttpURLConnection ) url.openConnection();
// Indicate that you will be doing input and output, that the method is POST, and that the content
// length is the length of the byte array
con.setDoOutput( true );
con.setDoInput( true );
con.setRequestMethod( "POST" );
con.setRequestProperty( "Content-length", String.valueOf( bytes.length ) );


// Create the Basic Auth Header

//Base64 encoder = new Base64();

BASE64Encoder encoder = new BASE64Encoder();

basicAuth = (String)encoder.encode( ((String)(userId + ":" + password)).getBytes() );

System.out.println("Codificado: " + basicAuth);

con.setRequestProperty( "Authorization", "Basic " + basicAuth.trim() );

// Write the parameters to the URL output stream
OutputStream output = con.getOutputStream();
output.write( bytes );
output.flush();




} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}
}

d@dora Profilo | Senior Member

Pongo, sei stato molto gentile, ma io devo tradurlo da JAVA a vb !!

Ciao e grazie

Pongo Profilo | Senior Member

Scusa non avevo letto bene!

Magari ero già ubriaco a quell'ora.

Cosi a vista non mi sembrava java e allora ho pensato di tradurlo in java.

VB lo evito con tutte le mie forze, è troppo brutto! ^_^

sorry.

d@dora Profilo | Senior Member

a fatica ho scritto quanto sotto, ma non mi funziona e non capisco il perche: mi potete dare una mano ?!?!?



Dim sHttp As String = "http://" & "192.168.150.56" & "/CGI/Execute"

Dim myCredentialCache As New CredentialCache

myCredentialCache.Add(New Uri(sHttp), "Basic", New NetworkCredential("cravinetto", "12345"))


Dim myHttpWebRequest1 As HttpWebRequest = CType(WebRequest.Create("http://" & "192.168.150.56" & "/CGI/Execute"), HttpWebRequest)
myHttpWebRequest1.Method = "POST"
myHttpWebRequest1.Credentials = myCredentialCache



Dim sStr As String = "<CiscoIPPhoneExecute>"
sStr += "<ExecuteItem Priority='0' URL='http://192.168.1.125/telefonia/sms/sms.xml' />"
sStr += "<ExecuteItem Priority='1' URL='Play:KotoEffect.raw' />"
sStr += "</CiscoIPPhoneExecute>"


Dim newStream As Stream = myHttpWebRequest1.GetRequestStream()
Dim encodedData As New System.Text.ASCIIEncoding

Dim byteArray As Byte() = encodedData.GetBytes(sStr)

newStream.Write(byteArray, 0, byteArray.Length)

newStream.Flush()
newStream.Close()
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5