WebRequest post transmission percent '% ' __web

Source: Internet
Author: User

When the data is transmitted through the WebRequest post, if the parameter in the URL contains '% ', the null pointer or error occurs when the parameter of the address bar is taken. such as the following child

string URL = Http://127.0.0.1/file.do;

String postdata = "domethod=list&content=30%";

byte[] data = Encoding.Default.GetBytes (postdata);

HttpWebRequest myrequest = (HttpWebRequest) webrequest.create (URL);

Myrequest. method = "POST";
Myrequest. ContentType = "application/x-www-form-urlencoded";
Myrequest. ContentLength = data. Length;
Stream stream = Myrequest. GetRequestStream ();

Send data
Stream. Write (data, 0, data. Length);
Stream. Close ();


Get response
HttpWebResponse response = (HttpWebResponse) myrequest. GetResponse ();
StreamReader reader = new StreamReader (response. GetResponseStream (), encoding.default);
From the example above, we see that the value of the content is 30%, then the parameter values obtained in the Java servlet after the run are null, which means that the% here is escaped, so how to solve it. We can go ahead and find out what the next hundred semicolon escapes. As follows:

String sss = Urlencoder.encode ("%%%", "GBK");
SYSTEM.OUT.PRINTLN (SSS);

In the console print is%25%25%25, from the results above you can see that the percent of the escape character is%25, then we just need to replace the content in the above example of the% to%25 to solve the problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.