3

我想在后面的代码中制作一个帖子表单。我有简单的 html 帖子正在工作,但是当我尝试使它成为 WebRequest 时,我无法使其工作。

提前感谢您的时间。

这是工作>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
</head>
<body>
<form action="http://www.pay-pos.com/test.php" method="post">
<textarea name="datafromuser">
<CC5Request><Name>Mert</Name><Password>123</Password><ClientId>1058</ClientId><IPAddress>213.456.654.456</IPAddress><Adress>asdsa</Adress><OrderId>123</OrderId><Type>Auth</Type>
<Number>4522115422659661</Number><ExpiresAy>01</ExpiresAy><ExpiresYil>13</ExpiresYil><Cvv2Val>123</Cvv2Val><Total>10</Total><Taksit></Taksit><Kdv>18</Kdv><BankaID>1</BankaID>
<TcKimlik>12345678912</TcKimlik></CC5Request>
</textarea>
<input type="submit" value="aaa" />
</form>
</body>
</html>

这不起作用>

String ali = "<CC5Request><Name>Mert</Name><Password>123</Password><ClientId>xxxx</ClientId><IPAddress>213</IPAddress><Adress>asdsa</Adress>" +
                        "<OrderId>123</OrderId><Type>Auth</Type><Number>1234567891234567</Number><ExpiresAy>01</ExpiresAy><ExpiresYil>13</ExpiresYil><Cvv2Val>123</Cvv2Val>" +
                        "<Total>10</Total><Taksit></Taksit><Kdv>xx</Kdv><BankaID>1</BankaID><TcKimlik>12345678912</TcKimlik></CC5Request>";
                    WebRequest req = WebRequest.Create("http://www.pay-pos.com/test.php?datafromuser=" + ali);
                    req.Credentials = CredentialCache.DefaultCredentials;
                    req.Method = "POST";
                    HttpWebResponse loWebResponse = (HttpWebResponse)req.GetResponse();
                    Encoding enc = Encoding.GetEncoding(1254); 

                    StreamReader loResponseStream =

                       new StreamReader(loWebResponse.GetResponseStream(), enc);

                    string lcHtml = loResponseStream.ReadToEnd();
                    loWebResponse.Close();
                    loResponseStream.Close();

编辑:实验 1

String postData = "Name=Mert" +
              "&Password=123" +
              "&ClientId=1058" +
              "&IPAddress=213.456.654.456" +
              "&Adress=asdsa" +
              "&OrderId=123" +
              "&Type=Auth" +
              "&Number=4522115422659661" +
              "&ExpiresAy=01" +
              "&ExpiresYil=13" +
              "&Cvv2Val=123" +
              "&Total=10" +
              "&Taksit=1" +
              "&Kdv=18" +
              "&BankaID=1" +
              "&TcKimlik=12345678912";

            string URI = "http://www.pay-pos.com/test.php";

            WebClient wc = new WebClient();
            wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
            string HtmlResult = wc.UploadString(URI, postData);

实验 2

 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.pay-pos.com/test.php");
        request.Method = WebRequestMethods.Http.Post;
        request.ContentLength = postData.Length;
        request.ContentType = "application/x-www-form-urlencoded";
        request.KeepAlive = false; //also tried true as well

        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(postData);
        writer.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        String responseString = reader.ReadToEnd();

        response.Close();

实验 3 !!!工作!!!

String ali = "datafromuser=<CC5Request><Name>Mert</Name><Password>123</Password><ClientId>1058</ClientId><IPAddress>213.456.654.456</IPAddress><Adress>asdsa</Adress><OrderId>123</OrderId><Type>Auth</Type>" +
"<Number>4522115422659661</Number><ExpiresAy>01</ExpiresAy><ExpiresYil>13</ExpiresYil><Cvv2Val>123</Cvv2Val><Total>10</Total><Taksit></Taksit><Kdv>18</Kdv><BankaID>1</BankaID>"+
"<TcKimlik>12345678912</TcKimlik></CC5Request>";

            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(ali);

            WebRequest req = WebRequest.Create("http://www.pay-pos.com/test.php");

            req.ContentType = "application/x-www-form-urlencoded"; 
            req.ContentLength = byteArray.Length;
            req.Method = "POST"; 

            Stream dataStream = req.GetRequestStream(); 
            dataStream.Write(byteArray, 0, byteArray.Length); 
            dataStream.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            String responseString = reader.ReadToEnd();

            response.Close();
4

4 回答 4

5

我看到的第一件事是你试图使用查询字符串来发布表单数据。您的“表单数据”应该是这样的:

 String ali = "datafromuser=<CC5Request><Name>Mert</Name><Password>123</Password><ClientId>xxxx</ClientId><IPAddress>213</IPAddress><Adress>asdsa</Adress>" +
                            "<OrderId>123</OrderId><Type>Auth</Type><Number>1234567891234567</Number><ExpiresAy>01</ExpiresAy><ExpiresYil>13</ExpiresYil><Cvv2Val>123</Cvv2Val>" +
                            "<Total>10</Total><Taksit></Taksit><Kdv>xx</Kdv><BankaID>1</BankaID><TcKimlik>12345678912</TcKimlik></CC5Request>";

接下来,您需要从表单数据中获取 bytes[]。

byte[] byteArray = Encoding.UTF8.GetBytes(ali);

设置一些标题:

req.ContentType = "application/x-www-form-urlencoded"; 
req.ContentLength = byteArray.Length;
req.Method = "POST"; 

现在将您的数据写入请求流。

Stream dataStream = req.GetRequestStream(); 
dataStream.Write(byteArray, 0, byteArray.Length); 
dataStream.Close(); 

终于……得到你的回应。还要注意,任何实现 IDisposable 的东西都应该包含在一个using语句中,即Streamand WebResponse

另请注意,提交按钮不是表单发布数据的一部分。服务器可能正在期待它。

编辑:这是 Microsoft 提供的完整示例,可逐步指导您。

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

于 2012-07-09T11:52:56.787 回答
1

这可能会帮助你。

string URI = "http://www.myurl.com/post.php";
string myParamters = "param1=value1&param2=value2";

WebClient wc = new WebClient();
wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
于 2012-07-09T11:50:15.573 回答
1

这是一个简单的例子。postData 取决于形式

String postData = "Name=" + Username +
              "&Password=" + Password +
              "&Retype=" + Password +
              "&Email=" + HttpUtility.UrlEncode(EmailAddress) +
              "&RealName=" + String.Format("{0}+{1}", FirstName.Replace(" ", "+"), LastName.Replace(" ", "+"));

现在创建请求并发布数据:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = postData.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = false;

StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(postData);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
String responseString = reader.ReadToEnd();

response.Close();

您可以使用 Fiddler 来捕捉 postData 的外观。

于 2012-07-09T11:56:17.757 回答
0

像这样试试。

using (WebClient client = new WebClient())
   {

       byte[] response = client.UploadValues("http://dork.com/service", new NameValueCollection()
       {
           { "home", "Cosby" },
           { "favorite+flavor", "flies" }
       });
   }

您将需要这些包括:

using System;
using System.Collections.Specialized;
using System.Net;

如果您坚持使用静态方法/类:

public static class Http
{
    public static byte[] Post(string uri, NameValueCollection pairs)
    {
        byte[] response = null;
        using (WebClient client = new WebClient())
        {
            response = client.UploadValues(uri, pairs);
        }
        return response;
    }
}

然后简单地说:

Http.Post("http://dork.com/service", new NameValueCollection() {
    { "home", "Cosby" },
    { "favorite+flavor", "flies" }
});
于 2012-07-09T12:32:54.603 回答