|
||||
|
|
#1 (permalink) |
|
Nuovo della community ![]()
8 Messaggi
![]() |
WebRequest - Inviare files in POST via HTTP
Ciao ragazzi ho questa difficoltà.
Ho bisogno di inviare in post un file binario insieme ad una stringa di testo ad un server in ascolto. Finchè invio solo la stringa tutto ok. Il problema è che non ho proprio idea di come poter inviare anche il file. Dalla documentazione in mio possesso, so che devo inviare il binario del file (una immagine nello specifico), ma non so proprio come ricavarmi questo binario e inserirlo in una variabile di tipo string che poi posso concatenare alla stringa di post. Allego il codice sorgente della classe in questione, nel caso in cui possa essere utile a qualcuno per venire a capo del problema. codice:
public static string SendMemeImmagine(string testo,
string immagine,
string fonte,
string canali,
string luogo)
{
string username = Settings.GetUsername();
string password = Settings.GetPassword();
string responseImg = string.Empty;
WebRequest request = WebRequest.Create("http://meemi.com/api/" + Settings.GetUsername() + "/save");
request.Method = "POST";
byte[] byteArrayImg = ConvertImageToByteArray((System.Drawing.Image)new Bitmap(immagine), ImageFormat.Jpeg);
string postData = "meemi_id=" + username +
"&pwd=" + Hash.sha256encrypt(password) +
"&app_key=" + Convert.ToString(ConfigurationSettings.AppSettings["APPKEY"]) +
"&meme_type=image" +
"&text_content=" + testo +
"&img_pc=" + System.Text.ASCIIEncoding.ASCII.GetString(byteArrayImg) +
"&fonte=" + fonte +
"&channels=" + canali +
"&location=" + luogo;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length; // Set the ContentLength property of the WebRequest.
Stream dataStream = request.GetRequestStream(); // Get the request stream.
dataStream.Write(byteArray, 0, byteArray.Length); // Write the data to the request stream.
dataStream.Close(); // Close the Stream object.
WebResponse response = request.GetResponse(); // Get the response.
Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Display the status.
dataStream = response.GetResponseStream(); // Get the stream containing content returned by the server.
StreamReader reader = new StreamReader(dataStream); // Open the stream using a StreamReader for easy access.
string responseFromServer = reader.ReadToEnd(); // Read the content.
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
__________________
Errare è umano, ma per fare veramente casino ci vuole la password di root ! |
|
|
|
|
|
#2 (permalink) |
|
Nuovo della community ![]()
8 Messaggi
![]() |
In pratica io devo concatenare a img_pc il binario dell'immagine, ma non ho proprio idea di come recuperarlo e trasformarlo in stringa.
ho copiaincollato qualcosa dal web ma non sono riuscito a concludere gran che.
__________________
Errare è umano, ma per fare veramente casino ci vuole la password di root ! |
|
|
|
|
|
#3 (permalink) | |
|
Very Important Person ![]() ![]()
894 Messaggi
![]() ![]() ![]() ![]() |
Quote:
non ho tempo di provare ma i metodi convert.tobase64string e convert.frombase64string mi sembrano promettenti HTH
__________________
Ciao sspintux ------------------------------------------------------------ Volevamo cambiare il mondo...peccato che abbiamo perso lo scontrino |
|
|
|
|
![]() |
«
Link a exe
|
Folderbrowser
»
| Strumenti della discussione | |
| Modalità di visualizzazione | |
|
|
Tutti gli orari sono GMT +2. Attualmente sono le 09:56.











Modalità lineare

