如何从.net发布对Java servlet的请求

时间:2017-02-14 06:30:40

标签: c# .net ccavenue

如何将以下数据发布到servlet url:

数据:

enc_request=63957FB55DD6E7B968A7588763E08B240878046EF2F520C44BBC63FB9CCE726209A4734877F5904445591304ABB2F5E598B951E39EAFB9A24584B00590ADB077ADE5E8C444EAC5A250B1EA96F68D22E44EA2515401C2CD753DBA91BD0E7DFE7341BE1E7B7550&access_code=8JXENNSSBEZCU8KQ&command=confirmOrder&request_type=XML&response_type=XML&version=1.1

网址:

"https://logintest.ccavenue.com/apis/servlet/DoWebTrans";

我试过了https://social.msdn.microsoft.com/Forums/vstudio/en-US/df76fbfa-2931-4c9f-8671-785307243f62/post-request-to-a-java-servlet-and-process-httpresponse-using-net?forum=wcf。但是我收到了错误:C# System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send

以下是我的代码:

                 StringBuilder sbQueryString = new StringBuilder();
                 CCACrypto ccaCrypto = new CCACrypto();
                 string workingKey = "14A246E9D4341159638AC8EB776F3BE6";

//put     in the   32bit alpha numeric key in the quotes provided here

                sbQueryString.Append("enc_request=");
                sbQueryString.Append(ccaCrypto.Encrypt(GenerateOrderXml(appointmentId), workingKey));
                sbQueryString.Append("&");
                sbQueryString.Append("access_code=");
                sbQueryString.Append("AVQJ68DL46BW00JQWB");
                sbQueryString.Append("&");
                sbQueryString.Append("Command=");
                sbQueryString.Append("cancelOrder");
                sbQueryString.Append("&");
                sbQueryString.Append("request_type=XML&response_type=XML");
                sbQueryString.Append("&");
                sbQueryString.Append("version=1.1");
                //Response.Redirect("../ccAvenue/ccavAPIHandler.aspx?" + sbQueryString.ToString(), false);

                //string url = "https://logintest.ccavenue.com/apis/servlet/DoWebTrans";
                string url = "https://180.179.175.17/apis/servlet/DoWebTrans";

                // Create a request using a URL that can receive a post.   
                WebRequest request = WebRequest.Create(url);
                // Set the Method property of the request to POST.  
                request.Method = "POST";
                // Create POST data and convert it to a byte array.  
                string postData = sbQueryString.ToString();
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                // Set the ContentType property of the WebRequest.  
                request.ContentType = "application/x-www-form-urlencoded";
                // Set the ContentLength property of the WebRequest.  
                request.ContentLength = byteArray.Length;
                // Get the request stream.  
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.  
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.  
                dataStream.Close();
                // Get the response.  
                WebResponse response = request.GetResponse();
                // Display the status.  
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.  
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.  
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.  
                string responseFromServer = reader.ReadToEnd();
                // Display the content.  
                Console.WriteLine(responseFromServer);
                // Clean up the streams.  
                reader.Close();
                dataStream.Close();
                response.Close();

1 个答案:

答案 0 :(得分:1)

您可以尝试使用WebClient

来简化代码
using (var client = new WebClient())
{
    var data = new NameValueCollection
    {
        { "enc_request", "63957FB55DD6E7B968A7588763E08B240878046EF2F520C44BBC63FB9CCE726209A4734877F5904445591304ABB2F5E598B951E39EAFB9A24584B00590ADB077ADE5E8C444EAC5A250B1EA96F68D22E44EA2515401C2CD753DBA91BD0E7DFE7341BE1E7B7550" },
        { "access_code", "8JXENNSSBEZCU8KQ" },
        { "command", "confirmOrder" },
        { "request_type", "XML" },
        { "response_type", "XML" },
        { "version", "1.1" },
    };
    var url = "https://logintest.ccavenue.com/apis/servlet/DoWebTrans";
    byte[] result = client.UploadValues(url, data);
}