0

Im trying to send some data to a machin via LAN cable and WebRequest this is the class im using

public class MyWebRequest
{
    private WebRequest request;
    private Stream dataStream;

    private string status;

    public String Status
    {
        get
        {
            return status;
        }
        set
        {
            status = value;
        }
    }

    public MyWebRequest(string url)
    {

        request = WebRequest.Create(url);
    }

    public MyWebRequest(string url, string method)
        : this(url)
    {

        if (method.Equals("GET") || method.Equals("POST"))
        {
            request.Method = method;
        }
        else
        {
            throw new Exception("Invalid Method Type");
        }
    }

    public MyWebRequest(string url, string method, string data)
        : this(url, method)
    {

        string postData = data;
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        request.ContentType = "application/x-www-form-urlencoded";

        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;

        dataStream = request.GetRequestStream();

        dataStream.Write(byteArray, 0, byteArray.Length);

        dataStream.Close();

    }

    public string GetResponse()
    {
        WebResponse response = request.GetResponse();

        this.Status = ((HttpWebResponse)response).StatusDescription;

        dataStream = response.GetResponseStream();

        StreamReader reader = new StreamReader(dataStream);

        string responseFromServer = reader.ReadToEnd();

        reader.Close();
        dataStream.Close();
        response.Close();

        return responseFromServer;
    }

}

and the action code is

    command="?insert_employee:cart_number=0007989222&name=علي&last_name=رمضاني&finger_number=123&?";
MyWebRequest wr = new MyWebRequest(command, "GET");
string Response = wr.GetResponse();

as you see there is some utf-8 character in my command and it dose not work both in browser and my application but when i try this string it works perfectly:

command = "?insert_employee:cart_number=0007989222&name=ali&last_name=ramazani&finger_number=123&?"

i tested many ways which i found in stackoverflow such as "using uri instead of url", "using httpUtility.UrlEncode", ... but none of them was effective or maybe i use them in a wrong way.

do you have any idea for helping?

sara m
  • 17
  • 4

1 Answers1

0

You can use the HttpUtility.UrlPathEncode method you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.

Whereas there is beautiful explanation of Encoding

urlPrueba = "http://someexample.com/?insert_employee:cart_number=0007989222&name=علي&last_name=رمضاني&finger_number=123&?";
var uri = new Uri(urlPrueba);
Console.WriteLine("urlPrueba" + " = " + uri.AbsoluteUri);
Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69