2012-09-07 59 views
0

我試圖發送XML文檔到頁面的.asp,並得到了答案,但我得到以下錯誤:發送一個網頁,腳本

System.UriFormatException: Invalid URI: The URI scheme is not valid. at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind) at System.Net.WebRequest.Create(String requestUriString) at GNS_ZalkarBank.GNSTaskServiceZalkarBank.CreateRequest(String requestData, String address) at GNS_ZalkarBank.GNSTaskServiceZalkarBank.SendRequest(String requestString, String address) at GNS_ZalkarBank.GNSTaskServiceZalkarBank.processData(TaskInfo& taskInfo, Object& data) at Task.RegistryTemplate.RegistryTaskTemplate.execute(DataSet& dataSet)`

我實現了一個方法,用於發送數據到一個頁面與ASP服務器腳本:

private string SendRequest(String requestString, String address) 
{ 
    address = "https://myadress/osmp_gni_xml.asp"; 
    HttpWebRequest httpRequest = this.CreateRequest(requestString, address); 
    string response = GetResponse(httpRequest); 
    return response; 
} 

private HttpWebRequest CreateRequest(string requestData, string address) 
{ 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); 
    request.Method = "POST"; 
    //request.UserAgent = "Test"; 

    byte[] data = Encoding.UTF8.GetBytes(requestData); 
    request.ContentType = "text/xml; encoding='utf-8'"; 
    request.ContentLength = data.Length; 
    using (Stream dataStream = request.GetRequestStream()) 
    { 
     dataStream.Write(data, 0, data.Length); 
     dataStream.Close(); 
    } 
    return request; 
} 

private string GetResponse(HttpWebRequest httpWebRequest) 
{ 
    string responseString; 
    HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse(); 
    using (Stream dataStream = response.GetResponseStream()) 
    { 
     using (StreamReader reader = new StreamReader(dataStream)) 
     { 
      responseString = reader.ReadToEnd(); 
     } 
    } 
    return responseString; 
} 

服務器端(腳本頁面:osmp_gni_xml.asp):

<%@ Language=VBScript CODEPAGE="65001"%> 
<%  
    Sub AddSubNode(Parent, Name, Value) 
     Set subNode = XMLDoc.createElement(Name) 
     Parent.appendChild(subNode) 
     subNode.appendChild(XMLDoc.createTextNode(Value)) 
    End Sub 

    Function Stream_BinaryToString(Binary, CharSet) 
     Const adTypeText = 2 
     Const adTypeBinary = 1 

     'Create Stream object 

     Dim BinaryStream 'As New Stream 
     Set BinaryStream = CreateObject("ADODB.Stream") 

     'Specify stream type - we want To save text/string data. 

     BinaryStream.Type = adTypeBinary 

     'Open the stream And write text/string data To the object 

     BinaryStream.Open 
     BinaryStream.Write Binary 

     'Change stream type To binary 

     BinaryStream.Position = 0 
     BinaryStream.Type = adTypeText 

     'Specify charset For the source text (unicode) data. 

     If Len(CharSet) > 0 Then 
      BinaryStream.CharSet = CharSet 
     Else 
      BinaryStream.CharSet = "us-ascii" 
     End If 

     'Open the stream And get binary data from the object 

     Stream_BinaryToString = BinaryStream.ReadText 
    End Function 

    result=300 
    OK="incomplete request" 
    Dim PostData 
    Dim biData 

    PostData = "" 
    If Request.TotalBytes>0 Then 
     biData = Request.BinaryRead(Request.TotalBytes) 

     PostData=Stream_BinaryToString(biData, "utf-8") 

     ProvStr = "Provider=sqloledb;Data Source=TEST;Initial Catalog=TESTOsmp;User Id=tests_osmp;Password=tests;" 

     Set Conn = Server.CreateObject("ADODB.Connection") 
     Conn.Open ProvStr 
     Set cmdUA = Server.CreateObject("ADODB.Command") 
     cmdUA.ActiveConnection = Conn 
     cmdUA.CommandText = "GNI_Import" 
     cmdUA.CommandType = 4 
     cmdUA.Parameters.Append cmdUA.CreateParameter("Reestr", 202, 1, 2000, PostData) 

     Set RS = cmdUA.Execute 
     result = RS("result") 

     RS.Close 
     Conn.Close 
     Set Conn = Nothing 
     Set RS = Nothing 
    End If 

    'Create XML 

    Set XMLDoc = Server.CreateObject("Microsoft.XMLDOM") 

    Set pi = XMLDoc.createProcessingInstruction("xml"," version=""1.0"" encoding=""utf-8""") 
    XMLDoc.appendChild(pi) 

    'Main 

    Set mainNode = XMLDoc.createElement("response") 
    XMLDoc.appendChild(mainNode) 

    If result=0 Then 
     OK="Ok" 
    Else 
     result=300 
     OK="incomplete request" 
    End If 

    AddSubNode mainNode, "result", result 
    AddSubNode mainNode, "comment", OK 
    Response.ContentType = "text/xml" 
    Response.Write XMLDoc.XML 

    Set mainNode = Nothing 
    Set XMLDoc = Nothing 
%> 

怎麼啦?

回答

0

所提供的錯誤文本來自這裏:

private HttpWebRequest CreateRequest(string requestData, string address) 
{ 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); // <- THIS LINE 
    ... 
} 

錯誤說明是抱怨地址https://myadress/osmp_gni_xml.asp是無效的,是https最可能的原因是連接問題。

試圖通過https連接,但沒有設置https服務器端在第一位是非常普遍的:驗證地址是否可達,安全設置是否正確,以及排序。像F12開發人員控制檯(IE9的一部分),FireBug(Firefox擴展)或Fiddler(桌面應用程序)等控制檯工具是瞭解外部連接時發生的情況的最佳工具。

+0

是的,你說得對的地方精確值,問題的URL地址。服務器使用https - 我沒有意識到。我只是要求客戶證書。 – Max

0

我懷疑你所提供的代碼是不是有問題的代碼,看到這一點:

private string SendRequest(String requestString, --> String address <--) 
{ 
    --> address = "https://myadress/osmp_gni_xml.asp"; <-- 
    HttpWebRequest httpRequest = this.CreateRequest(requestString, address); 
    string response = GetResponse(httpRequest); 
    return response; 
} 

你傳遞地址的方法,但再硬接線它在第一行。問題與地址格式有關,但它可能不會與此代碼一起存在。

可能發生的情況是,您正在從文件或數據庫中讀取地址,並將其視爲您正在看到的「https://myadress/osmp_gni_xml.asp」的人員,因爲您使用的是有缺陷的無法解析的機制,但代碼看到的是類似於:

https/:////myaddress//osmp_gni_xml.asp 

它給出了相同的錯誤。

在您的實際代碼,什麼地址在你重寫它sendRequest將

相關問題