.NET Framework Bookmark and Share   
 index > Network Class Library (System.Net) > Http post to a WML page
 

Http post to a WML page

I am using HttpWebRequest/HttpWebResponse to post data from .net. to a wml page.

I am getting internal server error 500.

Need to know why i am getting internal server error.

IS it becasue of wrongly populated request.

Below is my code.

///This is calling class.

string

firstname = Request.QueryString["FirstName"];

string lastname = Request.QueryString["LastName"];

try

{

BlackBerryPOC.

PostSubmitter post = new BlackBerryPOC.PostSubmitter();

post.Url =

"http://wportal.bankofamerica.com:80/Controller/ControllerServlet";

post.PostItems.Add(

"lastName", lastname);

post.PostItems.Add(

"firstName", firstname);

post.Type = BlackBerryPOC.

PostSubmitter.PostTypeEnum.Post;

string result = post.Post();

}

catch (Exception ex)

{

}

////Actual class.



public

class PostSubmitter

{

/// <summary>

/// determines what type of post to perform.

/// </summary>

public enum PostTypeEnum

{

/// <summary>

/// Does a get against the source.

/// </summary>

Get,

/// <summary>

/// Does a post against the source.

/// </summary>

Post

}

private string m_url = string.Empty;

private NameValueCollection m_values = new NameValueCollection();

private PostTypeEnum m_type = PostTypeEnum.Get;

/// <summary>

/// Default constructor.

/// </summary>

public PostSubmitter()

{

}

/// <summary>

/// Constructor that accepts a url as a parameter

/// </summary>

/// <param name="url">The url where the post will be submitted to.</param>

public PostSubmitter(string url)

:

this()

{

m_url = url;

}

/// <summary>

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

/// </summary>

/// <param name="url">the url for the post.</param>

/// <param name="values">The values for the post.</param>

public PostSubmitter(string url, NameValueCollection values)

:

this(url)

{

m_values = values;

}

/// <summary>

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

/// </summary>

public string Url

{

get

{

return m_url;

}

set

{

m_url =

value;

}

}

/// <summary>

/// Gets or sets the name value collection of items to post.

/// </summary>

public NameValueCollection PostItems

{

get

{

return m_values;

}

set

{

m_values =

value;

}

}

/// <summary>

/// Gets or sets the type of action to perform against the url.

/// </summary>

public PostTypeEnum Type

{

get

{

return m_type;

}

set

{

m_type =

value;

}

}

/// <summary>

/// Posts the supplied data to specified url.

/// </summary>

/// <returns>a string containing the result of the post.</returns>

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;

}

/// <summary>

/// Posts the supplied data to specified url.

/// </summary>

/// <param name="url">The url to post to.</param>

/// <returns>a string containing the result of the post.</returns>

public string Post(string url)

{

m_url = url;

return this.Post();

}

/// <summary>

/// Posts the supplied data to specified url.

/// </summary>

/// <param name="url">The url to post to.</param>

/// <param name="values">The values to post.</param>

/// <returns>a string containing the result of the post.</returns>

public string Post(string url, NameValueCollection values)

{

m_values = values;

return this.Post(url);

}

/// <summary>

/// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.

/// </summary>

/// <param name="postData">The data to post.</param>

/// <param name="url">the url to post to.</param>

/// <returns>Returns the result of the post.</returns>

private string PostData(string url, string postData)

{

HttpWebRequest request = null;

if (m_type == PostTypeEnum.Post)

{

Uri uri = new Uri(url);

request = (

HttpWebRequest)WebRequest.Create(uri);

request.Method =

"POST";

request.ContentType =

"application/x-www-form-urlencoded";

request.ContentLength = postData.Length;

request.Timeout = 1200000;

using (Stream writeStream = request.GetRequestStream())

{

UTF8Encoding encoding = new UTF8Encoding();

byte[] bytes = encoding.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;

}

/// <summary>

/// Encodes an item and ads it to the string.

/// </summary>

/// <param name="baseRequest">The previously encoded data.</param>

/// <param name="dataItem">The data to encode.</param>

/// <returns>A string containing the old data and the previously encoded data.</returns>

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));

}

Pl. let me know what was wrong in the request formation. Whenever i call GetResponse method of teh request, i am getting internal server error.




  •  
appaamma
BlackBerryPOC.PostSubmitter does not sound like a Microsoft product. You should ask the vendor of this class.

The .NET Framework classes for performing POST are System.Net.WebClient and System.Net.HttpWebRequest.
BinaryCoder
I don't see anything wrong in your code. HTTP Internal server error 500 means the error is on server, not with your request (otherwise it would start with 400). I suggestcreating simple HTML file with simple form containing the same action URL, POST method, and fields with exactly the same names and values you are trying to send with HttpWebRequest. Then open the HTML in the InternetExplorer and press your form's submit button and see wether you are getting the sameinternalserver error 500. If yes, then the problem is indeed in the server and notin your code.

Oleksii Prokopchuk [NCL]

You can use google to search for other answers

Custom Search

More Threads

• Rate limitation on reception of UDP messages
• Sending signed and encrypted S/MIME (PKCS 7) mail with .net
• SMTPClient is not sending Authentication for server with DIGEST-MD5 CRAM-MD5
• Socket disposing after some time
• Issue witn IsInRole() on server app
• How to locate Live Server URL
• getting active directory username
• calculate the Round Trip Time
• Developing a Lan crawler
• How can i do a POST request faster (httpWebRequest, VB.NET)