ios - 如何在objective-c中使用HttpGet类?

时间:2013-05-28 10:31:43

标签: iphone ios objective-c

我是iPhone开发的新手,在我解码后我从谷歌获取访问令牌,现在我只想检查他/她是否是注册用户。我在Android代码,我想把它转换成Objective-C。

class HttpHelper 
{
 private static String token = null;
 public static void setToken(String token) {
 HttpHelper.token = token;
}

public static String getUrlContentAsString(String url, boolean ignoreHeader)
{
 try 
 {
  HttpGet request =new HttpGet(url);
  DefaultHttpClient client = new DefaultHttpClient();
  if (!ignoreHeader && HttpHelper.token != null)
  request.addHeader(new BasicHeader("Authorization",HttpHelper.token));
  HttpResponse response = client.execute(request);
  HttpEntity responseEntity = response.getEntity();

  BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
  StringBuilder sb = new StringBuilder();
  while (true) 
  {
   String line = reader.readLine();

  if (line != null)
  sb.append(line);

  else
  break;
  }

 reader.close();

 return sb.toString();
}

catch (Exception e) {
Log.e(

"VerboActivity", e.getMessage());
}

 return null;
}

任何人都可以帮助我吗?提前谢谢。

1 个答案:

答案 0 :(得分:3)

试试这段代码

 NSMutableURLRequest *request=[[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];

设置标题使用 -     [请求setValue: headerValue forHTTPHeaderField: headerfieldType ];

设定身体用途 -     [请求setHTTPBody: body(NSData) ];

最后得到回复 -

NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request     returningResponse:&response error:nil];