-3

我是 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;
}

谁能帮我?提前致谢。

4

1 回答 1

3

试试这段代码

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

要设置标头,请使用- [request setValue: headerValue forHTTPHeaderField: headerfieldType ];

设置正文使用- [request setHTTPBody: body(NSData) ];

终于得到回应——

NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request     returningResponse:&response error:nil];
于 2013-05-28T10:35:08.483 回答