2009-08-10 9 views
0

の中に失敗したポスティングデータ私は私のC#アプリケーションに次のコードを使用しているC#

string verification_url = @"http://site.com/posttest.php?"; 
string verification_data = "test=524001A"; 
string result = string.Empty; 
result = Post(verification_url, verification_data); 

public string Post(string url, string data) 
{ 
    string result = ""; 
    try 
    { 
     byte[] buffer = Encoding.GetEncoding(1252).GetBytes(data); 
     HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(@url); 
     WebReq.Method = "POST"; 

     WebReq.ContentType = "application/x-www-form-urlencoded";    
     WebReq.ContentLength = buffer.Length; 
     Stream PostData = WebReq.GetRequestStream(); 

     PostData.Write(buffer, 0, buffer.Length); 
     PostData.Close(); 

     HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); 
     Stream Answer = WebResp.GetResponseStream(); 
     StreamReader _Answer = new StreamReader(Answer); 
     result = _Answer.ReadToEnd(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
    if (result.Length < 0) 
    result = ""; 

    return result; 

} 

サーバー側のPHPコード

<?php 
$test=$_REQUEST['test']; 
echo $test; 
?> 

postメソッドは常に空の値

を返します。助けてください私

+0

あなたのコードは正常に動作し正常に動作します

string verification_url = @"http://www.site.com/posttest.php?"; 

に次の行

string verification_url = @"http://site.com/posttest.php?"; 

を変更しました。おそらくファイアウォール、プロキシなどのネットワーク設定の問題があります。 –

答えて

0

は、私はそれが今で書かれたよう

1

try

<?php 
print_r($_REQUEST); 
?> 

生のREAREST値を表示する。あなたのC#コードでどこにテストを設定するのかはわかりません。

+0

print_r出力 アレイ ( ) –

+0

アクセスログを確認してください。私の推測では、リクエストはサーバーを狙っていません。多分Tudor Olariuの ".net permission"があなたの問題の鍵です。 – Rufinus

0

リクエストURLにパラメータとして検証データを追加する必要があります。

string verification_data = "test=524001A"; 
string verification_url = @"http://site.com/posttest.php?" + verification_data; 

その方法は、あなたの実際の要求URLは次のようになります。

http://site.com/posttest.php?test=524001A 
+0

しかし、これはちょうど彼がcontenttype/lengthヘッダーを設定しているため、get要求になります。 – Rufinus

+0

いいえ、そうではありません。彼はWebReq.Method = "POST"を設定しており、エンコーディングは "application/x-www-form-urlencoded"です。私を信頼してください、それはquerystringでデータをPOSTします、私は前にこれをしました。 –

+0

それは私の運がC#を必要としないことでした。それがうまくいくと言えば、私はあなたに信じます。それはちょうど私に奇妙に見えます。 – Rufinus

0

あなたはRequestStreamを閉じないようにしようか?

PostData.Close(); 

PS:これを行うには、必要な.net権限がありますか?

+0

私は上記の行をまだ空の結果を削除しました –