将值传递给XML文件

时间:2013-03-27 08:17:00

标签: asp.net xml vb.net

我有一个Web应用程序,可以将表单的内容发送到另一个站点。另外我可以毫无问题地传递登录信息,页面验证正常,但我不知道如何传递另一端所需的信息数组。该数组包含设置名称及其各自的设置值。我有这个在PHP工作,但不知道如何在ASP.net中做到这一点

在PHP中,我将它用于数组:

        $aParams = array(
    'customeremailaddress' => 'mr.test@test.com',
    'customername' => 'mr test',
    'exlibrisfile' => '@/ePubs/Ex-Libris.png'
);

发送数组:

    curl_setopt($rCurl, CURLOPT_POSTFIELDS, $aParams);

这是我最终得到的VB代码返回400错误请求错误:

            Dim postString As String = String.Format("customeremailaddress={0}&customername={1}&referenceid={2}&languagecode={3}&expirydays={4}", customeremailaddress, customername, referenceid, languagecode, expirydays)
    Dim postBytes As Byte() = Encoding.UTF8.GetBytes(postString)

    webRequest__1.ContentLength = postBytes.Length

    Dim requestStream As Stream = webRequest__1.GetRequestStream()
    requestStream.Write(postBytes, 0, postBytes.Length)

完整的代码:

        Public Sub TestConn()        

    Dim customeremailaddress As String = "mr.test@test.com"
    Dim customername As String = "mr test"
    Dim referenceid As String = "ordertest123"
    Dim languagecode As String = "1043"
    Dim expirydays As String = "30"

    Dim UserName As String = "testusername"
    Dim password As String = "testpassword"
    Dim siteCredentials As New NetworkCredential(UserName, password)

    Dim URLAuth As String = "http://service.someurl.com/process.xml"
    Dim postString As String = String.Format("customeremailaddress={0}&customername={1}&referenceid={2}&languagecode={3}&expirydays={4}", customeremailaddress, customername, referenceid, languagecode, expirydays)
    Dim postBytes As Byte() = Encoding.UTF8.GetBytes(postString)

    Const contentType As String = "application/x-www-form-urlencoded"
    System.Net.ServicePointManager.Expect100Continue = False

    Dim cookies As New CookieContainer()
    Dim webRequest__1 As HttpWebRequest = TryCast(WebRequest.Create(URLAuth), HttpWebRequest)
    webRequest__1.Method = "POST"
    webRequest__1.ContentType = contentType
    webRequest__1.CookieContainer = cookies
    webRequest__1.ContentLength = postBytes.Length
    webRequest__1.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"
    webRequest__1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    webRequest__1.Referer = "http://service.someurl.com/process.xml"
    webRequest__1.Credentials = siteCredentials

    Dim requestStream As Stream = webRequest__1.GetRequestStream()
    requestStream.Write(postBytes, 0, postBytes.Length)

    Dim requestWriter As New StreamWriter(webRequest__1.GetRequestStream())

    Dim responseReader As New StreamReader(webRequest__1.GetResponse().GetResponseStream())
    Dim responseData As String = responseReader.ReadToEnd()

    responseReader.Close()
    webRequest__1.GetResponse().Close()

End sub

1 个答案:

答案 0 :(得分:1)

这是我将数据发布到另一个网址的代码。 文本是表单url-econded字符串。 希望这可以帮助

C#

private string PostData(string text)
{
  WebRequest request = WebRequest.Create(url);
  request.Method = "POST";
  byte[] byteArray = Encoding.UTF8.GetBytes(text);
  request.ContentType = "application/x-www-form-urlencoded";
  request.ContentLength = byteArray.Length;

  using (Stream dataStream = request.GetRequestStream())
  {
    dataStream.Write(byteArray, 0, byteArray.Length);
  }

  using(WebResponse response = request.GetResponse())
  using (Stream dataStream = response.GetResponseStream())
  using (StreamReader reader = new StreamReader(dataStream))
  {
    return reader.ReadToEnd();
  }


}

VB.NET

Private Function PostData(text As String) As String
Dim request As WebRequest = WebRequest.Create(url)
request.Method = "POST"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(text)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length

Using dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
End Using

Using response As WebResponse = request.GetResponse()
    Using dataStream As Stream = response.GetResponseStream()
        Using reader As New StreamReader(dataStream)
            Return reader.ReadToEnd()
        End Using
    End Using
End Using


End Function