मैं एक आईफोन/आईपैड से एक पीएचपी सेवा में एक बहु-लाइन पोस्ट भेजने की कोशिश कर रहा हूं, समस्या यह है कि किसी कारण से, पोस्ट सामग्री-प्रकार एप्लिकेशन/एक्स-www-form-urlenconded प्रतीत होता है, (मैं इस Wireshark का प्रयोग करके) को पता चला
यह वास्तव में Wireshark पोस्ट पैकेट पर कब्जा का एक टुकड़ा है:
**Content-Type: application/x-www-form-urlencoded\r\n**
Content-Length: 324\r\n
[Content length: 324]
Connection: keep-alive\r\n
\r\n
[Full request URI: http://my.server.com/mobile/tools/authentication]
Line-based text data: application/x-www-form-urlencoded
--0xKhTmLbOuNdArY\r\n
Content-Disposition: form-data; name="login"\r\n
\r\n
[email protected]\r\n
--0xKhTmLbOuNdArY\r\n
Content-Disposition: form-data; name="password"\r\n
\r\n
somepassword\r\n
--0xKhTmLbOuNdArY\r\n
\r\n
--0xKhTmLbOuNdArY--\r\n
समस्या यह है कि सर्वर में मैं इस से लॉगिन और पासवर्ड चर को पढ़ने के लिए कोशिश कर रहा हूँ POST अनुरोध, लेकिन यह संभव नहीं है क्योंकि मुझे लगता है कि PHP सोचता है कि POST अनुरोध x-www-form-urlencoded
है, इसलिए यदि मैं authentication.php
सेट करने के लिए सेटअप करता हूं:
<?php
echo("<pre>")
print_r($_POST)
echo("</pre>")
?>
मैं इस मिल:
<pre>Array\n
(\n
[--0xKhTmLbOuNdArY\r\n
Content-Disposition:_form-data;_name] => "login"\r\n
\r\n
[email protected]\r\n
--0xKhTmLbOuNdArY\r\n
Content-Disposition: form-data; name="password"\r\n
\r\n
somepassword\r\n
--0xKhTmLbOuNdArY\r\n
\r\n
--0xKhTmLbOuNdArY--\r\n
\n
)\n
</pre>
यह स्पष्ट रूप से अच्छा नहीं है, के बाद से अगर मैं इस सरल एचटीएमएल फ़ॉर्म का उपयोग करके एक अनुरोध भेज:
<FORM action="http://my.server.com/mobile/tools/authentication.php" method="post">
<P>
<LABEL for="login">E-mail: </LABEL>
<INPUT type="text" name="login"><BR>
<LABEL for="password">pass: </LABEL>
<INPUT type="text" name="password"><BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>
एक से उम्मीद के रूप में मैं इस Wireshark का पता लगाने मिल फॉर्म:
Content-Type: application/x-www-form-urlencoded\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n
Accept-Encoding: gzip,deflate,sdch\r\n
Accept-Language: en-US,en;q=0.8\r\n
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n
Cookie: PHPSESSID=tpp10pbrdkkf5dg9qprlamfuu2\r\n
\r\n
[Full request URI: http://mymed2.sophia.inria.fr/mobile/tools/authentication.php]
Line-based text data: application/x-www-form-urlencoded
login=hello%40hello.com&password=somepassword
औरसे यह टेक्स्ट आउटपुट 0 authentication.php
<pre>Array\n
(\n
[login] => [email protected]\n
[password] => somepassword\n
)\n
</pre>
में इस तरह से मैं ऑब्जेक्टिव-सी और कोको
- (NSMutableURLRequest *) POSTRequestWithURL:(NSURL *)url andDataDictionary:(NSDictionary *) dictionary
{
// Create a POST request
NSMutableURLRequest *myMedRequest = [NSMutableURLRequest requestWithURL:url];
[myMedRequest setHTTPMethod:@"POST"];
// Add HTTP header info
// Note: POST boundaries are described here: http://www.vivtek.com/rfc1867.html
// and here http://www.w3.org/TR/html4/interact/forms.html
NSString *POSTBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];
// Add HTTP Body
NSMutableData *POSTBody = [NSMutableData data];
[POSTBody appendData:[[NSString stringWithFormat:@"--%@\r\n",POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Add Key/Values to the Body
NSEnumerator *enumerator = [dictionary keyEnumerator];
NSString *key;
while ((key = [enumerator nextObject])) {
[POSTBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[POSTBody appendData:[[NSString stringWithFormat:@"%@", [dictionary objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
[POSTBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
// Add the closing -- to the POST Form
[POSTBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Add the body to the myMedRequest & return
[myMedRequest setHTTPBody:POSTBody];
return myMedRequest;
}
में पोस्ट अनुरोध क्राफ्टिंग कर रहा हूँ जैसा कि आप देख है, शुरुआत में मुझे क्या करना:
[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];
लेकिन पिछले वायर्सहार्क ट्रेस लॉग में, पोस्ट पैकेट को सामग्री-प्रकार के रूप में देखा जा रहा था: एप्लिकेशन/एक्स-www-form-urlencoded, शायद मैं कुछ और गलत कर रहा हूं?
धन्यवाद :)
संपादित करें: मैं, here
केवल एक चीज मैं के बारे में सोच सकते हैं: क्यों आप मैन्युअल रूप से एक HTTP अनुरोध लिख रहे हैं? क्या एपीआई के लिए यह नहीं है? – CodeCaster
NSMutableURLRequest प्रदान की गई "एपीआई" है, ऐसा करने के लिए कुछ ढांचे हैं, लेकिन मैं बाहरी ढांचे से बचने की कोशिश कर रहा हूं। – Goles