Click here to Skip to main content
16,016,204 members
Home / Discussions / C#
   

C#

 
AnswerRe: Math.Round..What am I missing? Pin
led mike18-Dec-07 5:07
led mike18-Dec-07 5:07 
GeneralRe: Math.Round..What am I missing? Pin
jayart18-Dec-07 5:32
jayart18-Dec-07 5:32 
AnswerRe: Math.Round..What am I missing? Pin
dipak.dipak18-Dec-07 5:24
dipak.dipak18-Dec-07 5:24 
GeneralRe: Math.Round..What am I missing? Pin
Luc Pattyn18-Dec-07 5:55
sitebuilderLuc Pattyn18-Dec-07 5:55 
Questionquestion about FrameWork 3.5 Pin
E_Gold18-Dec-07 4:07
E_Gold18-Dec-07 4:07 
GeneralRe: question about FrameWork 3.5 Pin
Colin Angus Mackay18-Dec-07 4:31
Colin Angus Mackay18-Dec-07 4:31 
GeneralRe: question about FrameWork 3.5 Pin
Paul Conrad24-Dec-07 19:52
professionalPaul Conrad24-Dec-07 19:52 
QuestionHttpWebRequest ? Pin
cd_dvd18-Dec-07 3:59
cd_dvd18-Dec-07 3:59 
using System;
using System.Text;
using System.IO;
using System.Web;
using System.Net;
using System.Collections.Specialized;
using System.Web;

namespace Snowball.Common
{
///
/// Submits post data to a url.
///

public class PostSubmitter
{

public int byteleng;
///
/// determines what type of post to perform.
///

public enum PostTypeEnum
{
///
/// Does a get against the source.
///

Get,
///
/// Does a post against the source.
///

Post
}

private string m_url = string.Empty;
private NameValueCollection m_values = new NameValueCollection();
private PostTypeEnum m_type = PostTypeEnum.Get;
///
/// Default constructor.
///

public PostSubmitter()
{
}

///
/// Constructor that accepts a url as a parameter
///

/// <param name="url" />The url where the post will be submitted to.
public PostSubmitter(string url)
: this()
{
m_url = url;
}

///
/// Constructor allowing the setting of the url and items to post.
///

/// <param name="url" />the url for the post.
/// <param name="values" />The values for the post.
public PostSubmitter(string url, NameValueCollection values)
: this(url)
{
m_values = values;
}

///
/// Gets or sets the url to submit the post to.
///

public string Url
{
get
{
return m_url;
}
set
{
m_url = value;
}
}
///
/// Gets or sets the name value collection of items to post.
///

public NameValueCollection PostItems
{
get
{
return m_values;
}
set
{
m_values = value;
}
}
///
/// Gets or sets the type of action to perform against the url.
///

public PostTypeEnum Type
{
get
{
return m_type;
}
set
{
m_type = value;
}
}
///
/// Posts the supplied data to specified url.
///

/// <returns>a string containing the result of the post.
public string Post()
{
StringBuilder parameters = new StringBuilder();
for (int i = 0; i < m_values.Count; i++)
{
EncodeAndAddItem(ref parameters, m_values.GetKey(i), m_values[i]);
}
string result = PostData(m_url, parameters.ToString());
return result;
}
///
/// Posts the supplied data to specified url.
///

/// <param name="url" />The url to post to.
/// <returns>a string containing the result of the post.
public string Post(string url)
{
m_url = url;
return this.Post();
}
///
/// Posts the supplied data to specified url.
///

/// <param name="url" />The url to post to.
/// <param name="values" />The values to post.
/// <returns>a string containing the result of the post.
public string Post(string url, NameValueCollection values)
{
m_values = values;
return this.Post(url);
}
///
/// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
///

/// <param name="postData" />The data to post.
/// <param name="url" />the url to post to.multipart/form-data; boundary=
/// <returns>Returns the result of the post.
private string PostData(string url, string postData)
{
// string sa="";
HttpWebRequest request = null;
if (m_type == PostTypeEnum.Post)
{


Uri uri = new Uri(url);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytess = encoding.GetBytes(postData);
request.ContentType = "multipart/form-data; boundary=" + "---------------------" + DateTime.Now.Ticks.ToString("x"); //;//application/x-www-form-urlencoded
request.ContentLength = bytess.Length;// postData.Length;

using (Stream writeStream = request.GetRequestStream())
{
UTF8Encoding encoding1 = new UTF8Encoding();
byte[] bytes = encoding1.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
}
}
else
{
Uri uri = new Uri(url + "?" + postData);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
}
string result = string.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
return result;
}
///
/// Encodes an item and ads it to the string.
///

/// <param name="baseRequest" />The previously encoded data.
/// <param name="dataItem" />The data to encode.
/// <returns>A string containing the old data and the previously encoded data.
private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem)
{
if (baseRequest == null)
{
baseRequest = new StringBuilder();
}
if (baseRequest.Length != 0)
{
baseRequest.Append("&");
}
baseRequest.Append(key);
baseRequest.Append("=");

baseRequest.Append(System.Web.HttpUtility.UrlEncode(dataItem));
}
}
}


--------------

byte[] Image = (byte[])ImagePicture;

string picture1 = Convert.ToBase64String(Image);
PostSubmitter post = new PostSubmitter();
post.Url = "http://.....";
post.PostItems.Add("Body/ad/category_id", "2");
post.PostItems.Add("Body/ad/language_code", "1");
post.PostItems.Add("Body/ad/format", "1");
post.PostItems.Add("Body/ad/price_type", "1");
post.PostItems.Add("Body/ad/ask_price", "112345.67");
post.PostItems.Add("Body/ad/title", "TEST IT-Pictures");
post.PostItems.Add("Body/ad/description", "TEST IT-Pictures");
post.PostItems.Add("Body/image1", picture1);
post.Type = PostSubmitter.PostTypeEnum.Post;
string result = post.Post();
It outputs an error! Help, what's wrong, I think that erroc in
private string PostData(string url, string postData)
and exactly in this string:

request.ContentType = "multipart/form-data; boundary=" + "---------------------" + DateTime.Now.Ticks.ToString("x");

GeneralRe: HttpWebRequest ? Pin
Vasudevan Deepak Kumar18-Dec-07 4:12
Vasudevan Deepak Kumar18-Dec-07 4:12 
GeneralRe: HttpWebRequest ? Pin
cd_dvd18-Dec-07 4:31
cd_dvd18-Dec-07 4:31 
GeneralRuntime type casting Pin
Skippums18-Dec-07 3:23
Skippums18-Dec-07 3:23 
GeneralRe: Runtime type casting Pin
CKnig18-Dec-07 3:29
CKnig18-Dec-07 3:29 
GeneralRe: Runtime type casting Pin
CKnig18-Dec-07 3:32
CKnig18-Dec-07 3:32 
GeneralRe: Runtime type casting Pin
Skippums18-Dec-07 6:37
Skippums18-Dec-07 6:37 
GeneralRe: Runtime type casting Pin
m@u18-Dec-07 3:35
m@u18-Dec-07 3:35 
GeneralRe: Runtime type casting Pin
Skippums18-Dec-07 5:56
Skippums18-Dec-07 5:56 
GeneralDetermine User Role (Vista) Pin
Stevo Z18-Dec-07 3:10
Stevo Z18-Dec-07 3:10 
GeneralRe: Determine User Role (Vista) Pin
Peter Walburn26-Feb-10 4:38
Peter Walburn26-Feb-10 4:38 
GeneralTAPI 2 Pin
baerten18-Dec-07 3:09
baerten18-Dec-07 3:09 
GeneralConvert to int Pin
eyeseetee18-Dec-07 1:55
eyeseetee18-Dec-07 1:55 
GeneralRe: Convert to int Pin
J4amieC18-Dec-07 1:59
J4amieC18-Dec-07 1:59 
GeneralRe: Convert to int Pin
Pete O'Hanlon18-Dec-07 2:28
mvePete O'Hanlon18-Dec-07 2:28 
GeneralRe: Convert to int Pin
eyeseetee18-Dec-07 3:00
eyeseetee18-Dec-07 3:00 
GeneralRe: Convert to int Pin
Vasudevan Deepak Kumar18-Dec-07 3:01
Vasudevan Deepak Kumar18-Dec-07 3:01 
GeneralRe: Convert to int Pin
eyeseetee18-Dec-07 3:13
eyeseetee18-Dec-07 3:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.