Omlouvám se, ale zasílám příklad v C#. Místo "application/x-www-form-urlencoded", si nastavte "text/xml; charset=utf-8" V příklady jsou i výpisy HTTP hlaviček.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
//Do "Reference" přidáno "System.Net.Http" a "System.Net.Http.WebRequest"
namespace POST_Client_v2 {
class Program {
const bool UTF8 = false; //chci kódovat ?
static void Main( string[] args ) {
Console.WriteLine( "Posílá POST zprávu." );
if ( args.Length == 2 ) {
GetPage( args[ 0 ], args[ 1 ] );
}
else {
ShowUsage();
}
Console.WriteLine();
Console.WriteLine( "Stiskni libovolnou klavesu..." );
Console.ReadLine();
} //Main
private static void ShowUsage() {
Console.WriteLine( "Pouziti:" );
Console.WriteLine( " ClientPOST URL soubor" );
Console.WriteLine();
Console.WriteLine( "Priklad:" );
Console.WriteLine( " ClientPOST http://www.microsoft.com data_post.txt" );
} //ShowUsage
private static void ShowRequestHeader( WebRequest request ) {
Console.WriteLine( "WebRequest:" );
Console.WriteLine( "\tRequestUri\t: " + request.RequestUri.ToString() );
Console.WriteLine( "\tMethod\t\t: " + request.Method.ToString() );
Console.WriteLine( "\tContentType\t: " + request.ContentType.ToString() );
Console.WriteLine( "\tContentLength\t: " + request.ContentLength.ToString() );
Console.WriteLine( "\tTimeout\t\t: " + request.Timeout.ToString() );
Console.WriteLine( "\tHeaders\t\t: " + request.Headers.Count.ToString() );
for ( int i = 0; i < request.Headers.Count; i++ ) {
Console.WriteLine( "\t\t" + request.Headers.Keys[ i ] + "\t:\t" + request.Headers[ i ] );
}
Console.WriteLine();
} //ShowRequestHeader
private static async void ShowRequestHeader( HttpRequestMessage request ) {
Console.WriteLine( "HttpRequestMessage:" );
Console.WriteLine( "\tRequestUri\t: " + request.RequestUri.ToString() );
Console.WriteLine( "\tMethod\t\t: " + request.Method.ToString() );
Console.WriteLine( "\tVersion\t\t: " + request.Version.ToString() );
Console.WriteLine( "\tHeaders:" );
if ( request.Headers != null ) {
foreach ( var header in request.Headers ) {
foreach ( string value in header.Value ) {
Console.WriteLine( "\t\t" + header.Key + "\t:\t" + value );
}
}
}
Console.WriteLine();
Console.WriteLine( "\tContent:" );
Console.WriteLine( "\t\tContent length : " + request.Content.Headers.ContentLength );
Console.WriteLine( "\t\tContent type : " + request.Content.Headers.ContentType );
Console.WriteLine();
try {
byte[] byteBuffer = await request.Content.ReadAsByteArrayAsync();
Console.WriteLine( "----------------------------------------------" );
Console.WriteLine( OVa.Utils.BuffToStr_Dump( byteBuffer, 0, byteBuffer.Length ) );
Console.WriteLine( "----------------------------------------------" );
}
catch {
}
Console.WriteLine();
} //ShowRequestHeader
private static void ShowResponseHeader( WebResponse response ) {
Console.WriteLine( "WebResponse:" );
Console.WriteLine( "\tResponseUri\t: " + response.ResponseUri.ToString() );
Console.WriteLine( "\tContentType\t: " + response.ContentType.ToString() );
Console.WriteLine( "\tContentLength\t: " + response.ContentLength.ToString() );
Console.WriteLine( "\tIsFromCache\t\t: " + response.IsFromCache.ToString() );
Console.WriteLine( "\tIsMutuallyAuthenticated\t: " + response.IsMutuallyAuthenticated.ToString() );
Console.WriteLine( "\tHeaders\t\t: " + response.Headers.Count.ToString() );
for ( int i = 0; i < response.Headers.Count; i++ ) {
Console.WriteLine( "\t\t" + response.Headers.Keys[ i ] + "\t:\t" + response.Headers[ i ] );
}
Console.WriteLine();
} //ShowRequestHeader
private static async void ShowResponseHeader( HttpResponseMessage response ) {
Console.WriteLine( "HttpResponseMessage:" );
Console.WriteLine( "\tIsSuccessStatusCode\t: " + response.IsSuccessStatusCode.ToString() );
Console.WriteLine( "\tStatusCode\t\t: " + response.StatusCode + " .. " + (Int32)response.StatusCode );
Console.WriteLine( "\tVersion\t\t\t: " + response.Version.ToString() );
Console.WriteLine( "\tReasonPhrase\t\t: " + response.ReasonPhrase );
Console.WriteLine( "\tHeaders:" );
if ( response.Headers != null ) {
foreach ( var header in response.Headers ) {
foreach ( string value in header.Value ) {
Console.WriteLine( "\t\t" + header.Key + "\t:\t" + value );
}
}
}
Console.WriteLine();
Console.WriteLine( "\tContent:" );
Console.WriteLine( "\t\tContent length : " + response.Content.Headers.ContentLength );
Console.WriteLine( "\t\tContent type : " + response.Content.Headers.ContentType );
Console.WriteLine();
try {
if ( response.Content.Headers.ContentType.MediaType.Equals( "application/octet-stream" ) ) {
// Jestliže je vraceno bytové pole
byte[] byteBuffer = await response.Content.ReadAsByteArrayAsync();
Console.WriteLine( "----------------------------------------------" );
Console.WriteLine( OVa.Utils.BuffToStr_Dump( byteBuffer, 0, byteBuffer.Length ) );
Console.WriteLine( "----------------------------------------------" );
}
else {
// Jestliže je vracen řetězec
string pad = await response.Content.ReadAsStringAsync();
Console.WriteLine( "----------------------------------------------" );
Console.WriteLine( pad );
Console.WriteLine( "----------------------------------------------" );
}
}
catch {
}
Console.WriteLine();
} //ShowRequestHeader
private static async void GetPage( string url, string fileName ) {
//-----------------------------------------------------------
// Načíst data ze souboru
const int BUFFER_MAX_LEN = 4096;
byte[] byteBuffer = new byte[ BUFFER_MAX_LEN ];
int readed = 0;
try {
Console.WriteLine( "File : " + fileName );
using ( FileStream fs = new FileStream( fileName, FileMode.Open ) ) {
//do {
// readed = await fs.ReadAsync( byteBuffer, 0, BUFFER_MAX_LEN );
//
// if ( readed > 0 ) {
// Console.WriteLine( OVa.Utils.BuffToStr_Dump( byteBuffer, 0, readed ) );
// }
//} while ( readed > 0 );
//Čte se pouze jednou.
//Jestliže je soubor delší, čte se max. to, co se vejde do bufferu.
readed = await fs.ReadAsync( byteBuffer, 0, BUFFER_MAX_LEN );
Console.WriteLine( OVa.Utils.BuffToStr_Dump( byteBuffer, 0, readed ) );
}
Console.WriteLine();
Console.WriteLine( "Readed : " + readed );
}
catch ( Exception e ) {
Console.WriteLine( "The file " + fileName + " could not be read:" );
Console.WriteLine( e.Message );
}
Console.WriteLine();
//-----------------------------------------------------------
// Jestliže se ze souboru nic nepřečetlo, bude se odesílat zpráva bez dat.
//-----------------------------------------------------------
// Sestavit požadavek na Http server
HttpClient web = new HttpClient();
try {
//-----------------------------
// Hlavička Http zprávy.
//-----------------------------
web.DefaultRequestHeaders.Accept.Clear();
//request.ContentType = "application/x-www-form-urlencoded";
//request.ContentType = "application/text";
//request.ContentType = "application/json";
//request.ContentType = "text/xml; charset=utf-8";
//request.ContentType = "text/xml";
//request.ContentType = "text/plain";
//request.ContentType = "text/plain; charset=utf-8";
//request.ContentType = "application/octet-stream";
web.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue( "application/x-www-form-urlencoded" ) );
//web.DefaultRequestHeaders.Expect.Add( new NameValueWithParametersHeaderValue( "Expect", "100-continue" ));
web.DefaultRequestHeaders.ExpectContinue = true;
web.DefaultRequestHeaders.Connection.Add( "Keep-Alive" );
web.DefaultRequestHeaders.Host = ( new Uri( url ) ).Host;
web.Timeout = new TimeSpan( 0, 0, 30 ); // Hours, Minutes, Seconds
//-----------------------------
// Datový obsah zprávy
//-----------------------------
// HttpContent je abstract třída.
//StringContent content = null;
ByteArrayContent content = new ByteArrayContent( byteBuffer, 0, readed );
content.Headers.ContentType = new MediaTypeHeaderValue( "application/octet-stream" );
//-----------------------------
// Odeslání Http zprávy
//-----------------------------
Console.WriteLine( string.Format( "Sending {0} bytes to {1}", content.Headers.ContentLength, url ) );
Console.WriteLine();
// GET: web.GetAsync( url );
// POST: web.PostAsync( url, HttpContent )
HttpResponseMessage response = await web.PostAsync( url, content );
//-----------------------------
// Přijatá odpověď
//-----------------------------
if ( response.IsSuccessStatusCode ) {
Console.WriteLine( "Request:" );
ShowRequestHeader( response.RequestMessage );
Console.WriteLine();
Console.WriteLine( "Response:" );
ShowResponseHeader( response );
Console.WriteLine();
}
else {
Console.WriteLine( "Odpoved s chybou:" );
Console.WriteLine( response.StatusCode );
}
}
catch ( Exception ex ) {
Console.WriteLine( OVa.Utils.Exception_ToString( ex, "Neco se prihodilo:" ));
}
}
}
}
|