我目前正在開發一個Windows Phone上的移動應用程序,它要求我向位於服務器上的PHP文件發送請求。PHP文件在本地工作,但不在服務器上
我遇到了一個問題,即請求在服務器上拋出一個錯誤,但不是當我更改代碼來頁面本地PHP文件時。任何想法爲什麼?
這裏是我的請求代碼:
string sendString = "";
sendString += "&lat=" + (App.Current as App).mapCenter.Latitude;
sendString += "&lng=" + (App.Current as App).mapCenter.Longitude;
sendString += "&address=" + (App.Current as App).locationAddress;
if (checkNumber(CountryCode.Text, AreaCode.Text, Number.Text))
{
sendString += "&phone=" + CountryCode.Text + "" + AreaCode.Text + "" + Number.Text;
}
else
{
MessageBox.Show("Phone number invalid");
}
if (checkEmail(email.Text))
{
sendString += "&email=" + email.Text;
}
else
{
MessageBox.Show("Invalid Email");
}
sendString += "device=WindowsPhone";
sendString += (bool)prefsms.IsChecked ? "&contactMethod=sms" : "&contactMethod=email";
ListPickerItem selectedItem = (ListPickerItem)ServiceType.SelectedItem; //Cast listpickeritem into String
string content = (string)selectedItem.Content;
sendString += "&serviceType=" + content;
sendString += "&version=2.0";
sendString += "&language=" + CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
(App.Current as App).watcher.Stop();
sendData = sendString;
if ((checkNumber(CountryCode.Text, AreaCode.Text, Number.Text)) && (checkEmail(email.Text)))
{
//Send data to server
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost/test.php");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.BeginGetRequestStream(new AsyncCallback(RequestCallBack), myRequest);
}
else
{
}
private void RequestCallBack(IAsyncResult asyncresult)
{
HttpWebRequest myRequest = asyncresult.AsyncState as HttpWebRequest;
Stream dataStream = myRequest.EndGetRequestStream(asyncresult);
//Avoid trashing procedure
this.Dispatcher.BeginInvoke(delegate()
{
StreamWriter writer = new StreamWriter(dataStream);
writer.Write(sendData);
writer.Flush();
writer.Close();
//Expect a response
myRequest.BeginGetResponse(new AsyncCallback(ResponseCallBack), myRequest);
});
}
private void ResponseCallBack(IAsyncResult asyncresult)
{
HttpWebRequest myRequest = asyncresult.AsyncState as HttpWebRequest;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(asyncresult);
// Avoid trashing the procedure
this.Dispatcher.BeginInvoke(delegate()
{
// Retrieve response stream
Stream responseStream = myResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
// Show response
responseTxt = reader.ReadToEnd();
MessageBox.Show("" + responseTxt);
});
}
我送一個字符串到服務器,服務器的PHP代碼如下所示(測試PHP是完全一樣的):
require_once("includes/globals.php");
$vars = ($PRODUCTION)?$_POST:$_REQUEST;
$MINIMUM_VERSION = 2.0;
function validServiceType($type)
{
return $type == "taxi" || $type == "ladies" || $type == "limo" || $type == "handicap";
}
if(!isset($vars['version']) || ((float)$vars['version']) < $MINIMUM_VERSION)
{
echo "oldVersion";
}
elseif(isset($vars['lat'], $vars['lng'], $vars['phone'], $vars['language']))
{
/*do stuff */
if($data['contactMethod'] == "email" && $data['email'] == "")
{
echo "invalid";
die();
}
if(DB_AddHail($data))
echo "processing";
else if (($company = DB_GetReferralTaxi($data['lat'], $data['lng'], $data['serviceType'])))
{
/*do some more stuff*/
}
else
echo "sorry";
}
else
{
echo "invalid";
}
?>
我不斷收到一個OldVersion錯誤,但我已經將版本號設置爲2.0了......關於發生了什麼的任何想法?
更新:我現在已經能夠找出問題的根源; HttpWebRequest根本沒有發送任何數據到服務器;我的$ _POST只是一個空數組。任何想法爲什麼?
更新2:我得到的錯誤是錯誤302(「我使用的URI只是http://而不是https://)。但是在將協議更改爲https://之後,它仍然沒有':T的工作是我的代碼不正確的那部分
這段代碼是否與你的代碼相同?' return $ type ==「taxi」|| $ type ==「女士們」|| $ type ==「limo」|| $ type == handicap「;'如果是,請嘗試從結尾刪除'''! – undone
^耶逃跑障礙。 – Killrawr
對不起,這是我的一個錯字;障礙已經在實際的現場版 – docaholic