2016-11-07 45 views
0

我試着通過LAN電纜一些數據發送到MACHIN和WebRequest的 這是使用UTF-8字符的URL無論是在C#中的WebRequest和瀏覽器無法正常工作

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

} 

類IM和行動碼是

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

當你看到有一些UTF-8字符在我的命令,並將其劑量不能同時在瀏覽器中工作和我的應用程序 但是當我嘗試這個字符串它完美的作品:

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

我測試了很多方法,我發現在諸如「使用uri而不是url」,「使用httpUtility.UrlEncode」,但是沒有一個是有效的,或者我可能以錯誤的方式使用它們。

你對幫助有什麼想法嗎?

回答

0

可以使用HttpUtility.UrlPathEncode方法,你編碼一個URL的路徑部分,以保證一致的解碼URL,無論哪一個平臺或瀏覽器進行解碼。

鑑於存在的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); 
+0

美麗的解釋我已經嘗試過這樣過,但沒有成功! –

+0

uri也不幫我 –

相關問題