我目前正在Windows手机上开发移动应用程序,这需要我向位于服务器上的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://后,它仍然没有'我的代码是不正确的?
答案 0 :(得分:0)
你可以尝试,在以if(!isset($ vars ['version'])开头的行之前,添加:
echo "version: " . $vars['version'];
exit;
查看它是否向浏览器输出任何内容。可能是你正在使用的php版本不喜欢$ vars =($ PRODUCTION)?$ _ POST:$ _ REQUEST;语法,你可能需要手动完成,所以像
$vars['version']=$_POST['version'];
$vars['somethingElse']=$_POST['somethingElse']
等...
答案 1 :(得分:0)
确保你的5.4.3并使用phpinfo()运行你的脚本;去检查。尝试更改您的PHP / C#以接收/发送GET请求而不是POST请求。
通过这种方式,如果您获得预期结果,您可以至少测试以查看何时从浏览器发送php脚本请求,例如http://yourdomain.com/script.php?name=John+Smith&Age=23。在检查结果时,从浏览器的角度下载https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/以检查标头的工作方式。