Skip to content

Posting Special charectors using HttpWebRequest

Kiran Suthar

Asked Dec 24, 2005, 2:52 AM EST2 answers
Share
LinkedInX
Hi, I am using HttpWebequest. When i post text/HTML containing special charectors like "#" , post is successfully done. Can anybody help me? I am using following code, string strURL = "some text with special charectors"; HttpWebRequest HWReq = (HttpWebRequest)WebRequest.Create(strURL); HWReq.Timeout = (int)cbTimeout.SelectedItem * 1000; HWReq.UserAgent = "Mozilla/4.0 (compatible;MSIE 6.0b;Windows NT 5.0)"; HWReq.AllowAutoRedirect = true; HWReq.Method = "POST"; HWReq.ContentType = "application/x-www-form-urlencoded"; HWReq.CookieContainer = new CookieContainer(); HWReq.CookieContainer.Add(ccLogin1); HttpWebResponse HWResp = (HttpWebResponse)HWReq.GetResponse(); I am getting Error in the above last line. Thanks, Kiran Suthar.

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    What exception message you get? You should always post the exception message, because we can't tell what the problem is without it.

    If i understood right, you want a URI to have special character in it, like in GET message? For example http://www.someaddress.com/index.htm?variable=Specialchars##HERE#¤%

    URI itself cannot contain special characters, but you can put special characters in get message if you convert the address this way:

    string uri = "http://www.someuri.com/index.htm";
    string getData = "parameter=VALUE#####";
    uri= Uri.EscapeUriString(uri);
    getData = Uri.EscapeDataString(getData);
    uri = uri + "?" + getData;


    That converts URI to its escaped representation, and thus strURI is in legal URI format.

    0
  • 0
    Hello, Thanks for the reply. I have tried your suggestion but unfortunately my problem didnt solved. I am getting System.Net.WebException, The renote server returned an error:(500) Internal server error. Actually I am posting contents of an HTML file, I have observed that contents of only first few lines(of HTML file) are posted. I have used content type as "application/x-www-form-urlencoded" as well as "text/html" but both has not worked. My code is as below, StreamReader srTemplate = new StreamReader(FilePath); string strTemplate = srTemplate.ReadToEnd(); string URL = "http://myurl..."; URL=Uri.EscapeUriString(URL); string strQueryString ="?Data1=" + strTemplate strQueryString = Uri.EscapeDataString(strQueryString); URL = URL +"?"+ strQueryString; HttpWebRequest HWReq = (HttpWebRequest)WebRequest.Create(URL); HWReq.Timeout = 20000; HWReq.UserAgent = "Mozilla/4.0 (compatible;MSIE 6.0b;Windows NT 5.0)"; HWReq.AllowAutoRedirect = true; HWReq.Method = "POST"; HWReq.ContentType = "application/x-www-form-urlencoded"; HWReq.CookieContainer = new CookieContainer(); HWReq.CookieContainer.Add(ccLogin); HttpWebResponse HWResp = (HttpWebResponse)HWReq.GetResponse(); Error occurs at the above last line. Any suggestion? Thanks, Kiran Suthar.
    0