2011-06-10 54 views
1

我有應用,在其中我有一個Web服務器API。這是我的API如何使用HTTP POST方法iphone

http://192.168.0.68:91/JourneyMapperAPI?RequestType=[<EntityKey>]&Command=[GET|SET|NEW]&Token=[token]&param...n=value..n 

請求類型的查詢字符串來發布參數服務器API預計請求的實體名稱這可能是任何數據庫表。

命令在查詢字符串中應該指定需要對指定的請求類型(可能是GET SET或NEW或任何其他實體特定命令)執行的操作。

例如,我有一個註冊表格,允許用戶註冊。
請求類型的登記表是登記等登記表的提交按鈕點擊API請求將

http://192.168.0.68:91/JourneyMapperAPI?RequestType=Register&Command=NEW&firstname=rocky&lastname=singh&Username=rocky14&Password=[password]&Email=[email]; 

如何使用HTTP POST方法與所有這些參數和值在這麼張貼此請求,服務器API這些值將被保存在名爲register的服務器表中。請幫助我解決此問題。感謝

+0

可能重複的[如何創建Http發佈數據到Web服務器?](http://stackoverflow.com/questions/2251078/how-to-create-http-post-data-to-the-web-服務器) – Saurabh 2011-06-10 05:28:34

+0

您提供的示例不是HTTP-POST,但GET – vikingosegundo 2011-06-10 08:04:24

+0

嗨@vikingosegundo,請你解釋一下如何使用GET方法,因爲這是我的第一個Web項目。如何通過http將我的值發送到服務器數據庫GET方法。感謝 – Rocky 2011-06-10 08:52:56

回答

1

您可以使用以下函數將數據發佈到Web服務器。

-(void)callCommentWebService:(NSString *)pstrCommentXML{ 

    NSString *soapMsg = 
    [NSString stringWithFormat: 
    @"<?xml version=\"1.0\" encoding=\"utf-8\"?>" 
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" 
    "<soap:Body>" 
    "<getImageComment xmlns=\"http://tempuri.org/\">" 
    "<Commentsxml>%@</Commentsxml>" 
    "</getImageComment>" 
    "</soap:Body>" 
    "</soap:Envelope>", pstrCommentXML 
    ]; 


    //Create URL Request 
    NSURL *url = [NSURL URLWithString: @"http://www.website.com/website/WebService.asmx?op=getImageComment"]; 
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 

    //Populate Headers 
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; 
    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
    [req setHTTPMethod:@"POST"]; 
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; 

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 
    if (conn) 
    { 
     webData = [[NSMutableData data] retain]; 
    } 
} 

這裏, URL =你的服務器的Web服務的路徑

pstrCommentXML =你的XML,其格式上傳

然後你可以使用正在從服務器響應簡單的委託方法來定義文件。

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response 
{ 
    [webData setLength: 0]; 
} 

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 
{ 
    [webData appendData:data]; 
} 

- (void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error 
{ 
    [webData release]; 
    [connection release]; 
} 

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{ 

} 

希望你明白了。

+0

嗨Nishant,我沒有一個XML文件。實際上,我有一個註冊表單與6 textfields.When用戶在這些字段中輸入值,然後單擊提交按鈕的值必須發佈到名爲UserInformation.Thanks服務器數據庫表 – Rocky 2011-06-10 10:11:45

+0

嗨,是的,我明白你的觀點。但是對於在服務器上進行上傳,服務器會接受一些格式。 (如XML格式,JSON格式)。所以有什麼格式,你可以從應用程序端創建它。然後上傳它。如果有其他事情,請告訴我。 – 2011-06-10 10:15:43

+0

xml格式應該爲此創建。無需創建我無法將我的數據保存在服務器端。請您解釋一下。因爲這是我在Web服務器端的第一個項目。請幫助 – Rocky 2011-06-10 10:20:23